DATA Step, Macro, Functions and more

Macro functionality

Reply
Contributor
Posts: 34

Macro functionality

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,
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro functionality

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
Super User
Posts: 9,682

Re: Macro functionality

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
N/A
Posts: 0

Re: Macro functionality

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
Super User
Posts: 9,682

Re: Macro functionality

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
Ask a Question
Discussion stats
  • 4 replies
  • 154 views
  • 0 likes
  • 4 in conversation