DATA Step, Macro, Functions and more

IF then statement

Reply
Occasional Learner
Posts: 1

IF then statement

Hi guys I really neeed help with this Macro!

it seems like my If then statemet is not working properly:

%macro exploredata (data);

proc contents data = &data out = v (keep=type name);

run;

proc sql noprint;                             

*where type = 1 specifies the numerical variable;

select name into :m separated by ' ' from v where type = 1;

Quit;

proc means data = &data;

var &m;

output out = temp1 min=&m;

run;

proc means data=&data;

var &m;

output out = temp2 n =&m;

run;

proc means data =&data;

var &m;

output out=temp3 max =&M

run;

proc means data = &data;

output out = temp4 mean=&m;

run;

proc means data =&data;

output out = temp5 nmiss=&m;

run;

proc means data=&data;

var &m;

output out = temp6 median =&m;

run;

data tem_all;

set temp1 temp2 temp3 temp4 temp5 temp6;

run;

proc transpose data = tem_all out=trans (rename = (COL1=min COL2 = nmiss COL3 = max COL4 = mean COL5=nomiss COL6= median));

var &m;

run;

Data temf;

set trans;

per_miss = nomiss/(nomiss+nmiss);

ind_name = trim(_name_)||'_in';

ind_name = trim(_name_)||'_in';

medianstr = put(median,3.);

run;

data temf;

set temf;

%if per_miss  > 0.50 %then %do;

indicator = trim(ind_name)||'=('||trim(_name_)||'=.)';

run;

%end;

%else %if 0.10 <per_miss <= 0.50 %then %do;

indicator = trim(ind_name)||'='||trim(medianstr);

run;

%end;

%else %do;

indicator = 'none';

run;

%end;

proc contents data = temf out = temf1(keep= name type );

run;

data temf2;

set temf1;set temf;

drop name;

run;

proc sql;

select indicator into :mv separated by ';' from temf2;

quit;

%mend exploredata;

%exploredata(A);

Frequent Contributor
Posts: 138

IF then statement

Hi ,

You are using macro if condition for data variabl check.Please use data step if statement.

Data temf;

set trans;

per_miss = nomiss/(nomiss+nmiss);

ind_name = trim(_name_)||'_in';

ind_name = trim(_name_)||'_in';

medianstr = put(median,3.);

run;

data temf;

set temf;

if per_miss  > 0.50 then do;

indicator = trim(ind_name)||'=('||trim(_name_)||'=.)';

run;

end;

else if 0.10 <per_miss <= 0.50 then do;

indicator = trim(ind_name)||'='||trim(medianstr);

run;

end;

else do;

indicator = 'none';

run;

end;

New Contributor
Posts: 2

Re: IF then statement

You don't need macro statements in your data step, and you don't need to reread temf to set indicator.

Replace this code

---------------------------------------------------------
run;
data temf;
set temf;
%if per_miss  > 0.50 %then %do;
indicator = trim(ind_name)||'=('||trim(_name_)||'=.)';
run;
%end;
%else %if 0.10 <per_miss <= 0.50 %then %do;
indicator = trim(ind_name)||'='||trim(medianstr);
run;
%end;
%else %do;
indicator = 'none';
----------------------------------------------------------

with this code

----------------------------------------------------------
select;
  when per_miss > 0.5 indicator = trim(ind_name)||'=('||trim(_name_)||'=.)';
  when per_miss > 0.1 indicator = trim(ind_name)||'='||trim(medianstr);
  otherwise indicator = 'none';
  end;

Super User
Posts: 17,828

Re: IF then statement

When doing comparisons with decimal numbers in macros you need to use %sysevalf

ie

%sysevalf(per_miss>0.50)

However as indicated above your code doesn't require macro logic, only data step logic.

You should also look into the STACKODS option of proc means, you can

probably avoid multiple passes of proc means.

Super Contributor
Super Contributor
Posts: 3,174

Re: IF then statement

Some initial desk-checking with the addition of SAS statement would likely have been revealing about the "macro language compile logic" being used incorrectly:

OPTIONS MACROGEN SYMBOLGEN MLOGIC MPRINT;

This will raise evidence that the %IF / %THEN logic is not appropriate and instead must be DATA step language IF / THEN logic.

As well, adding PUTLOG _ALL_;   statements at points in the code-processing would have also been revealing.

Ask a Question
Discussion stats
  • 4 replies
  • 355 views
  • 0 likes
  • 5 in conversation