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

Hello,

 

SAS is running the following code but results are not being generated. I'm running Enterprise Guide 7.12.

 

%macro abc;
	data want;
		set have;
		%do num=1 %to 25;
			%if diag_code&num. in ("Z590", "Z591", "Z598") %then
				dcheck&num.=1;
		%end;
	run;
%mend;
%abc;
run;

 Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You could switch to arrays, as suggested.  Then you wouldn't need any macro language at all.  However, you can get macro language to do the job easily enough by cutting out a couple of percent signs:

 

%macro abc;
	data want;
		set have;
		%do num=1 %to 25;
			if diag_code&num. in ("Z590", "Z591", "Z598") then
				dcheck&num.=1;
		%end;
	run;
%mend;
%abc

The idea is to have the DATA step process the data.  The role of macro language is to make sure that the correct DATA step code gets generated.

View solution in original post

4 REPLIES 4
Reeza
Super User
What happens if you use an array instead of macro code here?
Why are you using macros instead of arrays?
Astounding
PROC Star

You could switch to arrays, as suggested.  Then you wouldn't need any macro language at all.  However, you can get macro language to do the job easily enough by cutting out a couple of percent signs:

 

%macro abc;
	data want;
		set have;
		%do num=1 %to 25;
			if diag_code&num. in ("Z590", "Z591", "Z598") then
				dcheck&num.=1;
		%end;
	run;
%mend;
%abc

The idea is to have the DATA step process the data.  The role of macro language is to make sure that the correct DATA step code gets generated.

Kurt_Bremser
Super User

@torvyle wrote:

Hello,

 

SAS is running the following code but results are not being generated. I'm running Enterprise Guide 7.12.

 

%macro abc;
	data want;
		set have;
		%do num=1 %to 25;
			%if diag_code&num. in ("Z590", "Z591", "Z598") %then
				dcheck&num.=1;
		%end;
	run;
%mend;
%abc;
run;

 Thanks


Do not use the macro language for manipulating data. Macro language is for manipulating code.

Use the data step's tools for this:

data want;
set have;
array diags {*} diag_code1-diag_code25;
array checks {*} dcheck1-dcheck25;
do num = 1 to 25;
  if diags{num} in ("Z590", "Z591", "Z598")
  then checks{num} = 1;
end;
drop num;
run;
torvyle
Calcite | Level 5

Thanks, everyone.  KurtBremser's solution works too. 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 455 views
  • 0 likes
  • 4 in conversation