DATA Step, Macro, Functions and more

Missing Imputation Macro

Reply
Regular Contributor
Posts: 184

Missing Imputation Macro

I have a macro that replaces missing values with median. I tried to add 'mean' as a option type in macro. If user selects mean, it replaces missing values with mean. Otherwise, median.

It has been working fine. It started throwing errors since i use %IF %THEN %DO in the macro.

Errors :

Macro keyword MACRO appears as text.  A semicolon or other delimiter may be missing.

Macro keyword IF appears as text.  A semicolon or other delimiter may be missing.

options source2 mprint mlogic symbolgen;

%macro replace (input= prac.file1,analysis=,vars=Q3-Q5,output=replaced);

%if trim((left(&analysis)) = median %then %do;

proc means data=&input noprint;

var &vars;

output out=dummy (drop=_type_ _freq_) median= / autoname;

run;

%end;

%else %do;

proc means data=&input noprint;

var &vars;

output out=dummy (drop=_type_ _freq_) mean= / autoname;

run;

%end;

proc transpose data=dummy out=dummy;

run;

data _null_;

set dummy end=last;

call symput('med'||left(_n_),col1);

if last then call symput('maxi',_n_);

run;

data &output;

set &input;

array vars (*) &vars;

%do i =1 %to &maxi;

if vars(&i) = . then vars(&i)=&&med&i;

%end;

run;

%mend;

%replace (input= prac.file1,analysis= mean,vars=Q1-Q5,output=replaced);

Super User
Super User
Posts: 7,083

Re: Missing Imputation Macro

You probably have misplaced * or quotes that has SAS session confused.  If you cannot get it fixed then just close it and open a new one.

Your %IF condition will never be true because the string that started with the text TRIM( will never equal the string median.  You do not normally need to trim macro variables anyway.

But you are working too hard.  Here is a version that will eliminate the macro logic.

%macro replace (input= prac.file1,analysis=,vars=Q3-Q5,output=replaced);

* Generate analysis results ;

proc means data=&input noprint;  

  var &vars; 

  output out=dummy1 (drop=_type_ _freq_) &analysis= ; 

run; 

* Convert to vertical ;

proc transpose data=dummy1 out=dummy2 prefix=_analysis;

run;

* Replace missing with analysis results ;

data &output; 

  set &input; 

  array _vars &vars ;

  do p=1 to dim(_vars);

    set dummy2(keep=_analysis1) point=p ;

    _vars(p)=coalesce(_vars(p),_analysis1);

    drop _analysis1 ;

  end;

run;  

%mend;

options mprint nosymbolgen nomlogic ;

data test;

  set sashelp.class ;

  if _n_=4 then age=.;

run;

%replace

(input=test

,analysis= mean

,vars=_numeric_

,output=replaced

); 

proc compare data=sashelp.class compare=replaced;

run;

Regular Contributor
Posts: 184

Re: Missing Imputation Macro

Thank you so much for your reply. I have just reopened sas session and removed TRIM(LEFT( and it worked like a charm. Could you please help me understand the reason? How TRIM(LEFT caused problem to the macro?


"Your %IF condition will never be true because the string that started with the text TRIM( will never equal the string median.  You do not normally need to trim macro variables anyway."



Super User
Super User
Posts: 7,083

Re: Missing Imputation Macro

Most likely you left some previous statement open because of unbalanced quotes or some other problem,   So then every time after that it thinks the %MACRO statement is inside the middle of the previous expression.

The problem with your %IF condition is really separate.  If you wanted the macro to trim the value then you would need to use a macro function such as %TRIM().  To the macro the string TRIM() is just a bunch a characters. So TRIM(median) will never be equal to median since T does not equal m.

If you really did want to test if macro variable ANALYSIS had the value median then you would just do something like this:

%macro test(x) ;

  %if median = &x %then %put Found ;

  %else %put Not Found ;

%mend test ;

%test();

Not Found

%test(median);

Found

%test(mean);

Not Found

But you might need to watch out for the case of the supplied values.

%test(Median);

Not Found

Ask a Question
Discussion stats
  • 3 replies
  • 686 views
  • 0 likes
  • 2 in conversation