DATA Step, Macro, Functions and more

variable name corresponding to macro variable

Reply
Super Contributor
Posts: 673

variable name corresponding to macro variable

How to print the cecode that corresponds to &&var&i?
What happens is that i is taking the next value instead of the current value.
say &&var&i for i=1 resolves to 'AMPS' and for i=2 resolves to 'ACOP'. i want to print cecode as 'AMPS' when it reads values from AMPS dataset ,ACOP when it reads values from ACOP dataset in ref library.The records are appended in the set statement and a final dataset vir is created.

%macro cecodes;
data vir;
set
%do i=1 %to &nobs.;
ref.&&var&i
%end;;
cecode=&&var&i;

run;
%mend cecodes;
%cecodes;
Valued Guide
Posts: 634

Re: variable name corresponding to macro variable

You are close. try the following untested code in sas9.2 or earlier:
[pre]%macro cecodes;
data vir;
set
%do i=1 %to &nobs.;
ref.&&var&i(in=in&&var&i)
%end;;

%do i=1 %to &nobs.;
%if &i gt 1 %then else;
if in&&var&i then cecode="&&var&i";
%end;
run;
%mend cecodes;
%cecodes [/pre]

A second %DO builds the if-then/else. Notice the double quotes around the constant value for CECODE.
Super Contributor
Super Contributor
Posts: 3,174

Re: variable name corresponding to macro variable

Where is &nobs assigned - also, is it intentional that you have reference to &i outside your %DO loop? Lastly, suggest adding this code to help SAS generate the most diagnostics output for your desk-checking with SAS compilation:

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MPRINT /* MLOGIC */;

And, with the above code added, suggest the OP re-post a reply with the SAS-generated log output revealed, not just the code-piece -- presuming the problem is still not solved with additional log diagnostics info.

Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 673

Re: variable name corresponding to macro variable

PROC SQL;
select codes into:var1-:var20 from cecodes;
select count(*) into:nobs from cecodes;
QUIT;
%put &nobs;
Super Contributor
Super Contributor
Posts: 3,174

Re: variable name corresponding to macro variable

And, so, take this opportunity to debug your SAS program, as suggested, with the additional OPTIONS statement no less.

Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 673

Re: variable name corresponding to macro variable

I will look into it.Thanks.
Ask a Question
Discussion stats
  • 5 replies
  • 140 views
  • 0 likes
  • 3 in conversation