Wednesday, September 23, 2009

Use of %STR, %EVAL and %SYSEVALF


This is a code used for the programming of adverse event table (where a programmer has to repeatedly use FREQ). Here in this macro, one can notice the use of %str.

What is the use of %str in this code???

This helps in masking of special characters in the parameter value. The use of %str help the macro processor understand that the passed parameter value is “nothing other than text”.

For example: if the following code*** is executed without a %str and if a parameter value 'Body system' is passed to the macro, it does not comprehend the meaning of &txt = 'Body system’!
***
%if %str
(&txt) eq 'Body system' %then %do;

Instead the log displays a message saying that the "condition is false".

Hence the use of %str is absolutely necessary when parameter value contains blank/ special characters.

Same way, %eval is for evaluation of integers and %sysevalf is for floatingpoint values.

***** A CODE FOR FINDING FREQUENCY *****;

%macro freq(indt=, sort=, cond=, byvar=, class=, var=, ord=, txt=, outdt=);
proc sort data=&indt out=sdat; by &sort;
proc means data=sdat completetypes nway chartype missing noprint;
where &cond;
by &byvar;
class &class /preloadfmt missing mlf order=formatted;
var &var;
format trtcd trtf.;
output out=_freq n=ccnt mean = mn;
run;

data freq; length txt txt1 txt2 col1 $200;
set _freq;

ord=⩝ txt=&txt;

%if &ord=4 %then %do;
%if %str(&txt) eq 'Body system' %then %do;
txt1=strip(aebodsys); txt2=''; ord1=1;
%end;
%if %str
(&txt) eq 'Preferred term' %then %do;
txt1=strip(aebodsys); txt2=strip(aedecod); ord1=2;
%end;
%end;

%else %do;
txt1=''; txt2=''; ord1=0;
%end;

if trtcd=1 then deno = &trt1;
else if trtcd=2 then deno = &trt2;
else deno = &trt3;

if ord in(3,4,5) then do; per = (ccnt/deno)*100;
col1 = right(put(ccnt,3.))right(put('('trim(left(put(per,4.1)))'%)',8.));
end;
else if ord in (2) then do; col1 = right(put(mn,4.1)); end;
else if ord in (1) then do; col1 = right(put(mn,5.2)); end;
else do; col1 = right(put(ccnt,3.)); end;
run;

***** TRANSPOSED FOR DISPLAY *****;

proc sort data=freq; by txt txt1 txt2 ord ord1;
proc transpose data=freq out=freq1;
by txt txt1 txt2 ord ord1;
id trtcd ;
var col1;
run;

data &outdt;
set freq1(drop=_name_);
run;
%mend freq;

%freq(indt=zer, sort= trtcd, cond = , byvar= trtcd, class= , var=v, ord=0, txt='Subjects treated', outdt=zer1);