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
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.
Ready to level-up your skills? Choose your own adventure.