I am struggling with how I can create output when I am using a macro do loop in SAS. In a regular data step, I can create the following dataset with variables i
and var
to be "printed" in the output window by doing:
data example;
do i=1 to 4;
var = 2+i;
output;
run;
Which will create, in the output window,
Obs i var
1 1 3
2 2 4
3 3 5
4 4 6
Now, my issue is that I am using a macro do loop and can't seem to do the same as in a regular data step (i.e. can't get my result to show in the output window). The goal of my macro is to add "_new" to the end each variable name (A B C D) from the list, and then I need this to show up in the output window.
%let myvarlist=A B C D;
%macro create_new;
%let mlength=%countlength(&myvarlist);
%let suffix=new;
%do n=1 %to &mlength;
%let var_n=%qscan(&myvarlist,&n);
%let my_new_varlist=&var_n._&suffix;
%end;
%mend;
%create_new
The closest thing I could get that worked was putting %put &my_new_varlist;
right before the %end
statement, but this puts it in the log window. I tried inserting my do loop within a data step and then using a proc print but that didn't work.
The macro %countlength
is a macro I use to count the number of variables in my list. The code for it is:
%macro countlength(char);
%local num inputword;
%let num=1;
%let inputword=%qscan(&char,&num,%str( ));
%do %while(&inputword NE);
%let num=%eval(&num+1);
%let inputword=%qscan(&char,&num,%str( ));
%end;
%eval(&num-1);
%mend;