Hi Everyone,
I copied a code from SAS book and somehow it is not working.
The idea is to save all Name in a macro variable and print it one by one.
Can you please help?
Thanks,
Harry
proc sql noprint;
select name into :namelist1-
from sashelp.class
; quit;
%macro doprint;
%do i=1 %to &sqlobs;
%put name &i is &namelist1&i;
%end;
%mend;
%macro doprint;
%do i=1 %to &sqlobs;
%put name &i is &&namelist&i;
%end;
%mend;
%doprint
But I must ask, why do you need macro variables and macro do loops for this? Why not simply this:
proc sql;
select name from sashelp.class
; quit;
which is soooooo much easier than printing via a macro do loop.
Or why not simply this:
proc print data=sashelp.class;
var name;
run;
which is soooooo much easier than printing via a macro do loop.
You should not resort to macros just to print data that is already in a SAS data set, this is completely unnecessary and counter-productive.
%macro doprint;
%do i=1 %to &sqlobs;
%put name &i is &&namelist&i;
%end;
%mend;
%doprint
But I must ask, why do you need macro variables and macro do loops for this? Why not simply this:
proc sql;
select name from sashelp.class
; quit;
which is soooooo much easier than printing via a macro do loop.
Or why not simply this:
proc print data=sashelp.class;
var name;
run;
which is soooooo much easier than printing via a macro do loop.
You should not resort to macros just to print data that is already in a SAS data set, this is completely unnecessary and counter-productive.
Hi Paige,
I am learning that Macro code from SAS book.
But still, you code only give me the first row (i=1).
Thanks,
Harry
I just tried @PaigeMiller's code, and it seems to be working for me. Make sure you run both steps together, one right after another. Maybe you could just cut and paste the follwing code into an editor window:
proc sql noprint;
select name into :namelist1 -
from sashelp.class;
quit;
%macro doprint;
%do i=1 %to &sqlobs;
%put name &i is &&namelist&i;
%end;
%mend;
%DoPrint;
Make sure that "namelist" in "&&namelist&i" is spelled correctly. Originally, you spelled it "namelist1" which will not work properly.
Jim
Thank you for helping.
I got it!
Harry
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.