BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
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;
5 REPLIES 5
ArtC
Rhodochrosite | Level 12
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
SASPhile
Quartz | Level 8
PROC SQL;
select codes into:var1-:var20 from cecodes;
select count(*) into:nobs from cecodes;
QUIT;
%put &nobs;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
And, so, take this opportunity to debug your SAS program, as suggested, with the additional OPTIONS statement no less.

Scott Barry
SBBWorks, Inc.
SASPhile
Quartz | Level 8
I will look into it.Thanks.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 5 replies
  • 808 views
  • 0 likes
  • 3 in conversation