SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

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;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
%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.

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
%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.

--
Paige Miller
hhchenfx
Rhodochrosite | Level 12

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

jimbarbour
Meteorite | Level 14

@hhchenfx,

 

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

 

hhchenfx
Rhodochrosite | Level 12

Thank you for helping.

I got it!

Harry

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2878 views
  • 1 like
  • 3 in conversation