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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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