How to automatically determine lengths and display length frequencies in SAS

asked 2017-05-12 14:44:41 -0600

Hello I am trying to automate this. We routinely receive data in unknown formats. We bring it in as character, we create length variables to display. We are looking for the max as well as the frequencies. Typing this up each time is cumbersome. I wondered if there was a macro for this.

I have worked up some sample code here any help to make this more efficient would be appreciated

/***************Setting initial lengths****************************/
/***************Creating number as a character variable************/
data lengths (keep=CLIENT SERVER DESC NUMBER LEN_CLIENT LEN_SERVER LEN_DESC LEN_NUMBER);
length     CLIENT $ 100
           SERVER $ 100
           DESC   $ 100
           NUMBER $ 100;
INFORMAT   CLIENT $100.
           SERVER $100.
           DESC   $100.
           NUMBER $100.
                        ;
SET SASHELP.MONIKER;
           NUMBER="10";
/*************************left-aligning all character strings and determining lengths************************/
            CLIENT          = LEFT   (CLIENT    )                          ;
            SERVER          = LEFT   (SERVER    )                          ;
            DESC            = LEFT   (DESC      )                          ;
            NUMBER          = LEFT   (NUMBER    )                          ;
            LEN_CLIENT      = LENGTH (CLIENT        ) * (CLIENT         NE " ") ;
            LEN_SERVER      = LENGTH (SERVER        ) * (SERVER         NE " ") ;
            LEN_DESC        = LENGTH (DESC          ) * (DESC           NE " ") ;
            LEN_NUMBER      = LENGTH (NUMBER        ) * (NUMBER         NE " ") ;

/*************************looking at the distribution of lengths*********************************************/
/*************************the distribution of the lengths is equally informative to the max******************/
PROC FREQ   DATA = lengths ;
TABLES      LEN_CLIENT
            LEN_SERVER
            LEN_DESC
            LEN_NUMBER
                        ;
RUN;
/**********************Setting the variable to the Max Length**************************************************/
/**********************Creating Number as a numeric variable***************************************************/
DATA WORK.FINAL (KEEP = CLIENT SERVER DESC NUMBER) ;
LENGTH      CLIENT   $ 51
            SERVER   $ 30
            DESC     $ 60
            NUMBER    8.
                          ;
SET lengths  (RENAME = (NUMBER    = VAR_A));
           NUMBER            = INPUT (VAR_A,  COMMA2.)      ;
RUN                                                         ;
edit retag flag offensive close merge delete