1
votes

I have several variables in data set survey. I want to write a loop to load each variable into a SAS macro.

the code is below.

%let var= r1 r2 r3 ; 

DATA survey; 
   INPUT id sex $ age inc r1 r2 r3 ; 
   DATALINES; 
 1  F  35 17  7 2 2 
17  M  50 14  5 5 3 
33  F  45  6  7 2 7 
49  M  24 14  7 5 7 
65  F  52  9  4 7 7 
81  M  44 11  7 7 7 
2   F  34 17  6 5 3 
18  M  40 14  7 5 2 
34  F  47  6  6 5 6 
50  M  35 17  5 7 5 
; 

%MACRO bvars(input);

proc univariate data = "D:\hsb2" plots;
var &input.;
run;

%MEND bvars;

I just want &var can load into macro bvars each time for only one variable instead of writing the following.

%bvars(r1)
%bvars(r2)
%bvars(r3)
.....

This is time consuming while the number of variables are bigger than 100.

2
If you don't specify a var statement proc univariate will run for all numeric variables. Or try var numeric for all numeric variables. That numeric should have an underscore before and after but SO is eating it :(Reeza
That sort of useful but not really either, going through that much output manually would be painful, what are you really after in the end?Reeza

2 Answers

1
votes

This will run proc univariate for all the variables in survay which start with "r" (so r1, r2, etc.). Procedures with a var statement usually accept multiple variables.

proc univariate data = survey;
    var r:;
run;

If you wish to run for all numeric variables replace r: with _NUM_.

If you want to loop through the variables and call a function seperately each time there are several approaches. Usually they involve a macro do loop (which must be inside a macro) like so:

%macro looper(inData);
    /* List all the variable names */
    proc contents data = &inData. out = _colNames noprint;
    run;
    proc sql noprint;
        select name 
        /* Put the variable names in a macro variable list */
        into :colNames separated by " "
        from _colNames
        /* Get only numeric variables */
        where type = 1
        order by varnum;
    quit;
    /* Loop through the variable names */
    %do i = 1 %to %sysfunc(countw(&colNames.));
        %let colName = %scan(&colNAmes., &i.);
        %put &colName.;
        /* Your macro call or code here */
        /* %bvars(&inData., &colName.) */
    %end; 
%mend looper;
%looper(sashelp.cars);

It might prove useful for you to become familiar with macro %do loops, proc contents (or better yet proc datasets), the %scan() function and the different ways to assign macro variables. The sas documentation online is a great place to start.

-1
votes

Updated answer.

You can utilise the VCOLUMN table that is automatically created for every SAS dataset in each library including the Work library. This table contains a row for each variable for each dataset in SAS.

So you would do the following. I am assuming your survery dataset is in the Work library.

So the code does the following; 1. Looks ups your dataset in the Vcolumn table and only keep the name of the variable (thats all we need) and store it into dataset temp. 2. For every variable run the bvars Marcro via the call execute statement.

data temp(keep=name);
set Sashelp.Vcolumn;
where libname = 'WORK' and memname = 'SURVEY';
run;

*Call macro using call execute;
data _null_;
set temp;
call execute ("%bvars("||name||");");
run;