Reputation: 39
data freq;;
input placebo ;
cards;
3
;
Run;
data freqt;
set freq;
%macro freq1 (arm=,pct=,result=);
%if &arm ne . %then &pct=&arm*100;
%if &pct ne . %then %do;
%if &pct le 10 %then &result = "test";
%end;
%mend freq1;
%freq1(arm=placebo,pct=pct_pla,result=placebo_);
run;
Above data step macro If then conditions are not working but normal condition is working but don't need normal condition. Would like use only macro condition. Please help me.
Thank you...
Upvotes: 0
Views: 80
Reputation: 63424
You're confusing macro syntax with data step syntax. In macro syntax, you can refer only to the contents of the macro variable - not the data step variable. So if &arm
contains the variable name placebo
, then your condition
%if &arm ne . %then ...
tests whether the text placebo
equals .
. Of course it does not - so that's always false.
However, this works:
if &arm ne . then ...
Because now you're writing data step syntax, which allows you to access the data step variable placebo
. So now you compare 3
to .
(or whatever is in it).
You have some other issues; the macro definition shouldn't be inside the data step (it's possible to put it there, but it's sort of nonsense to do so) and of course your pct
logic has the same issues.
Upvotes: 1
Reputation: 51566
Hard to tell what you want to do. But to develop a macro you need to first start with working SAS code. Perhaps something like:
data freqt;
set freq;
if placebo ne . then pct_placebo=placebo*100;
if . <= pct_placebo <= 10 then placebo_= "test";
run;
Then replace the parts that vary with macro variable references.
%let arm=placebo;
...
if &arm ne . then pct_&arm=&arm*100;
if . <= pct_&arm <= 10 then &arm._= "test";
Then define a macro.
%macro freq(arm);
if &arm ne . then pct_&arm=&arm*100;
if . <= pct_&arm <= 10 then &arm._= "test";
%mend freq;
Then use the macro. You probably will want to use it multiple times otherwise it wasn't worth the trouble of making a macro to begin with.
data freqt;
set freq;
%freq(placebo)
%freq(drug1)
%freq(drug2)
run;
Upvotes: 1