/* This program demonstrates how you can use the symput command in SAS's macro language to loop through a dataset and perform an operation in SAS's datastep upon each element in the loop. This is the kind of stuff you have to do many times in other languages, but in SAS you are forced to use something like this to work on two datasets at once. You can't live without the SYMPUT statement when programming in SAS. This program is for illustrative purposes to demonstrate the methodology using symput. However, there are more efficient ways of getting the end result (different tables and their means) if that is what you are really interested in. */ data tmp; input BAD VAR1 VAR2 VAR3; cards; 1 10 33 4 0 20 21 3 1 30 59 2 1 20 76 1 0 . 24 3 0 20 22 2 1 10 28 2 1 10 33 4 0 20 21 3 1 30 59 2 1 20 76 1 0 . 24 3 0 20 22 2 1 10 28 1 1 10 49 1 0 30 76 2 1 10 33 4 0 20 21 3 1 30 59 2 1 20 76 1 0 . 24 3 0 20 22 2 1 10 28 1 1 10 49 1 0 30 76 1 1 20 59 2 1 20 59 2 1 10 49 2 0 30 76 2 1 20 59 2 ; /* You could get the table below by using 'proc sort nodupkey' on the previous dataset, but this is for illustration purposes. The table below will be used to cycle (loop) through the above dataset and pick out elements which will be written to separate files. */ data mytable; input VAR3; cards; 1 2 3 4 ; %macro illustrate_symput; OPTIONS NOTES SOURCE SOURCE2 MPRINT MLOGIC MERROR SYMBOLGEN; /* Get the number of values for VAR3 that you want to work with; and by using the _null_ command, you don't have to write it; out to a dataset.*/ data _null_; set mytable end=last; if last then call symput('nfiles',_n_); run; /* We use symput to cycle through 'nfiles' observations; to ouput separate files. In other programming languages you simply read in different files into memmory and work on them together. In SAS, you are limited in working in a single data step. Using symput via the macro language do loop is a work around for this in SAS. However, when using the symput command, SAS does not pick up the value (i.e. resolve) the function until AFTER the datastep is complete! */ %do i=1 %to &nfiles; data _null_; set mytable; /* The line of code below is the key here */ if &i=_n_ then call symput('myvalue',VAR3); run; /* Now we take it to the big dataset and do our task of outputting; the dataset to different files depending on the values */ data x_&i; set tmp; if VAR3=&myvalue then output ; run; /* Just for fun we can compute the bad rates for each in a table */ *Now we determine the bad rate; proc means mean data=x_&i noprint; VAR bad; OUTPUT OUT=meandat mean=mymean ; run; data _null_; set meandat end=last; call symput('mymean',trim(left(mymean))); run; data _null_; set x_&i end=last; if last then call symput('myobs',trim(left(_n_))); run; data mytable; set mytable; if &i=_n_ then bad_rate=&mymean; if &i=_n_ then nbr_obs=&myobs; run; title Output Tables (x_&i); proc print data=x_&i;run; %end; title Bad Rate Summary Table; proc print data=mytable;run; %mend illustrate_symput; *Executing Macro...; %illustrate_symput;