BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

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
Barite | Level 11

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
Barite | Level 11

Thank you for helping.

I got it!

Harry

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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