DATA Step, Macro, Functions and more

Macro help

Reply
Super Contributor
Posts: 272

Macro help

Dear,

 

I am able to call the macro variables values created by datastep(withoutmacro) where ever I can by calling "&b1,&c1,&d1,&e1".

 

But the macro variables created by macro step(shown below), I am not able call the macro variables. When I use "&b1,&c1,&d1,&e1" in a datastep,the values of the macrovariables are not called

 

Please help how can I use the macro variables created by the  macro step below. Thank you very much

 

data one;
input NS trt01a $ ;
datalines;
45 150MG
71 300MG
15 75MG
131 TOTAL
;
%macro two(var1=,var2=,var3=,var4=,var5=,var6=,var7=);
data &var2;
set &var1;
if trt01a='75MG' then trt="&var3";
if trt01a='150MG' then trt="&var4";
if trt01a='300MG' then trt="&var5";
if trt01a='TOTAL' then trt="&var6";
run;


data &var7;
set &var2;
call symputx(trt,ns);
run;
%mend;
%two(var1=one,var2=two,var3=b1,var4=c1,var5=d1,var6=f1,var7=three);
data one;
input NS trt01a $ ;
datalines;
45 150MG
71 300MG
15 75MG
131 TOTAL
;
data withoutmacro;
set one;
if trt01a='75MG' then trt="b1";
if trt01a='150MG' then trt="c1";
if trt01a='300MG' then trt="d1";
if trt01a='TOTAL' then trt="e1";
run;
data withoutmacro1;
set withoutmacro;
call symputx(trt,ns);
run;  

 

 

PROC Star
Posts: 7,467

Re: Macro help

Posted in reply to knveraraju91

You didn't show any code where you were attempting to apply macro variables outside of the macro, thus we can only guess.

 

Possibly, you simply have to define the macro variables you want to use outside of the macro as GLOBAL. Otherwise, they will simply be local to the macro.

 

Art, CEO, AnalystFinder.com

 

Super Contributor
Posts: 272

Re: Macro help

Thank you very much. % global statemnt worked.  Thank you

Super User
Posts: 19,769

Re: Macro help

Posted in reply to knveraraju91

I think you need to start over. I would bet money on the method you're using here is inefficient and clunky. It looks like you're trying to transpose/recode using macros when other options are probably better. 

 

Can you start from scratch by posting what you have and what you need? Make it small/simple enough to use on the forum, but complex enough to capture all your requirements.

Super Contributor
Posts: 272

Re: Macro help

Thank you very much for the reply. Some one suggested me to use "%global b1 c1 d1 f1;" in the macro. I think I got my answer. Thank you very much for the outstanding support

Super User
Super User
Posts: 7,039

Re: Macro help

Posted in reply to knveraraju91

Works fine for me.

2317  data one;
2318  input NS trt01a $ ;
2319  datalines;

NOTE: The data set WORK.ONE has 4 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


2324  ;
2325  data withoutmacro;
2326    set one;
2327    if trt01a='75MG' then trt="b1";
2328    if trt01a='150MG' then trt="c1";
2329    if trt01a='300MG' then trt="d1";
2330    if trt01a='TOTAL' then trt="e1";
2331    call symputx(trt,ns);
2332  run;

NOTE: There were 4 observations read from the data set WORK.ONE.
NOTE: The data set WORK.WITHOUTMACRO has 4 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


2333
2334  %put &=b1 &=c1 &=d1 &=e1;
B1=15 C1=45 D1=71 E1=131
Ask a Question
Discussion stats
  • 5 replies
  • 159 views
  • 1 like
  • 4 in conversation