Help using Base SAS procedures

SAS MACROS

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

SAS MACROS

%macro icodes ;
	
	proc sql ;
		create table _01_itable2 as
		%do i = 1 %to 14 ;
			select distinct enrolid, dx&i. as code 
			from derived._01_itable 
			%if i <14 %then %do ;
				union
			%end ;
		%end ;
		;
	quit;

%mend icodes ;
%icodes;	

Whats wrong with this code ?

SAS is giving me this error :- NOTE: Line generated by the invoked macro "ICODES".
129 select distinct enrolid, dx&i. as code from derived._01_itable
________ ____
78 22
76
ERROR 78-322: Expecting a ','.


Accepted Solutions
Solution
2 weeks ago
Super User
Super User
Posts: 8,120

Re: SAS MACROS

You are comparing the letter i to the digits 14

i < 14

Instead of testing the value of the macro variable i

&i < 14

View solution in original post


All Replies
Super User
Posts: 6,785

Re: SAS MACROS

Even if you get this to work, it seems extremely inefficient to read your data set 14 times just to be able to transpose the data.  Why not just use PROC TRANSPOSE?

Contributor
Posts: 53

Re: SAS MACROS

Posted in reply to Astounding

how to do that ? I'd like to see how to do it both by my code (macros) and proc transpose

Respected Advisor
Posts: 3,054

Re: SAS MACROS

Hint:

 

Use 

 

options mprint;

before calling the macro, then the error message will be much easier to debug.

 

Hint 2:

 

When including a SASLOG into this forum, use the {i} icon that will space the letters properly so we can see exactly what is underlined.

--
Paige Miller
Solution
2 weeks ago
Super User
Super User
Posts: 8,120

Re: SAS MACROS

You are comparing the letter i to the digits 14

i < 14

Instead of testing the value of the macro variable i

&i < 14
Super User
Super User
Posts: 8,120

Re: SAS MACROS

[ Edited ]

Just use PROC transpose

proc transpose data=derived._01_itable 
  out= _01_itable2 (rename=(col1=code)) 
;
 by enrolid ;
  var dx1-dx14 ;
run;

Although if you really do have multiple records per ENROLID you might need to first decide which one you want to transpose.

Or create a new variable that will uniquely id the rows that you can use in the BY statement for PROC TRANSPOSE and then add a PROC SORT NODUPKEY to remove the multiple copies of the same ENROLID, CODE combinations.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 193 views
  • 4 likes
  • 4 in conversation