Search code examples
sas

How do I get this table through SAS?


|Var |Type     |Miss_val|Dis_val|
|Make|Character|0       | 38    |

I'd like the code to return just one clean table with four columns as shown above. My current code doesn't achieve that objective. Help would be appreciated. Thank you.

This is what I've tried so far but it doesn't work fully:


%macro task_Oct(set,col_name);

proc sql; 

    select nmiss(&col_name) as Miss_val, count(distinct &col_name) as Dis_val

    from &set;

quit;

proc contents

data=&set (keep=&col_name);

run;

%mend task_Oct;

%task_Oct(sashelp.cars,Make)


Solution

  • You're fairly close. Use the VTYPE() function with a data step to get the variable type (or query it from sashelp.vcolumn) or there are several other ways.

    It returns C for character and N for missing.

    %macro task_Oct(set,col_name);
    
    data _null_;
    set &set. (obs=1);
    call symputx('col_type', vtype(&col_name));
    run;
    
    proc sql; 
    
        select "&col_name" as var, 
                "&col_type" as type, 
                nmiss(&col_name) as Miss_val, 
                count(distinct &col_name) as Dis_val
    
        from &set;
    
    quit;
    
    %mend task_Oct;
    
    %task_Oct(sashelp.cars,Make)