Search code examples
sasproc-format

Using output of Proc Freq variables for input into Proc Format


I want to grab the number of variable levels as well as the variable for the output of the unique identifiers but currently my method does not work. I want to then use the unique ID's and associate numbers 1-num_levels from proc freq.

Here is what I have for proc freq:

PROC FREQ DATA=my_data (keep=IDs) nlevels;
table all/out=out_data;
%let dim=levels;
%let IDs;
run;

Then I tried to use the macro variables but it didn't work so I am including the manual version of my proc format to give a good idea of what I am trying to achieve but hopefully trying to make it more automated.

PROC FORMAT; 
INVALUE INDEX
"1234" = 1
"2345" = 2
.
.
.
"8901" =25;
/*25 represents the output of the levels 
variable from proc freq but I couldn't figure out how to grab that either*/
RUN;

Any help would be appreciated. Thank you!


Solution

  • Here's a fully worked solution, that illustrates a the PROC FORMAT CNTLIN way of doing this. The idea here is to mask names with the observation number instead.

    *Create list of unique names;
    proc freq data=sashelp.class noprint;
        table name/out = mask;
    run;
    
    *create control data set. Variables that need to be set are:
       fmtname, start, label and type;
    
    data name_fmt;
        set mask;
        fmtname = 'namefmt';
        type='J';
    
        *J specified character informat, C would be character format;
        start=name;
        label = put(_n_, z2.); *Use the row number as the recoded value;
    run;
    
    *create the format;
    proc format cntlin=name_fmt;
    run;
    
    *Sample usage;
    data class;
        set sashelp.class;
        name_masked = input(name, $namefmt.);
        drop name;
    run;