BookmarkSubscribeRSS Feed
Ujjawal
Quartz | Level 8

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);

3 REPLIES 3
Tom
Super User Tom
Super User

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;

Ujjawal
Quartz | Level 8

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."



Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1699 views
  • 0 likes
  • 2 in conversation