BookmarkSubscribeRSS Feed
DPraba79
Calcite | Level 5
Hi All,

I have a situation like, I have two macro varibales "Y2008" & "Y2009" with the values of 7 , 3 respectively. on the otherside, my data files has one variable "YEAR_END" and two obervarion with the values of 2008 & 2009. I have to resolve this YEAR_END vairbal with the above macro values (7 ,3) .

Please do see my code,

%let Y2008 = 7 ;
%let Y2009 = 3 ;

data data1;
input YEAR_END $4.;
out_var = &Y.&YEAR_END. ; <-- here i am getting error
cards;
2008
2009
;
run;

I want the output of data1 is,
out_var = 7 YEAR_END=2008
out_var =3 YEAR_END=2009

Kindly note, i don't want to hard code if condition , like, if YEAR_END=2008 then out_var = &Y2008
Thanks,
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
What you are wanting to use is "macro variable" resolution within your DATA step.

So, you want the SYMGET function within your DATA step and concatenate the character "Y" with the "input character variable YEAR_END.

Scott Barry
SBBWorks, Inc.


Suggested Google advanced search argument, this topic / post:

data step programming symget function site:sas.com Message was edited by: sbb
Ksharp
Super User
Hi. call execute is very useful for your situation.


[pre]
%let Y2008 = 7 ;
%let Y2009 = 3 ;

data _null_;
input YEAR_END $4.;

if _n_ eq 1 then call execute('data data1;');
call execute('out_var=&Y'||year_end||';');
call execute('year_end='||year_end||';');
call execute('output;');

datalines;
2008
2009
;
run;
[/pre]



Ksharp
deleted_user
Not applicable
Hello,

you can try the resolve function with single quotes in order to delay the resolution until the DATA step executes:

[pre]
data data1;
input YEAR_END $4.;
out_var=input(resolve(cats('&Y',YEAR_END)),best12.);
cards;
2008
2009
;
run;
[pre]

Marius
Ksharp
Super User
Great!
Which remaind me another simple solution.

[pre]

%let Y2008 = 7 ;
%let Y2009 = 3 ;
data data1;
input YEAR_END $4.;
out_var=symgetn('Y'||year_end);
cards;
2008
2009
;
run;
[/pre]

Ksharp

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
  • 4 replies
  • 845 views
  • 0 likes
  • 4 in conversation