BookmarkSubscribeRSS Feed
Anthonyroger
Calcite | Level 5

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

4 REPLIES 4
manojinpec
Obsidian | Level 7

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;

wannabeguru
Calcite | Level 5

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;

Reeza
Super User

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.

sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1057 views
  • 0 likes
  • 5 in conversation