DATA Step, Macro, Functions and more

choose unique proc/data steps based on value of variable

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

choose unique proc/data steps based on value of variable

i have a variable that has 2 values.  for value a, i want to do a series of procs and data steps.  for value b, i want to do a different set of procs/data steps.  both sets of procs/data steps are in 1 sas program.  how to i do this


Accepted Solutions
Solution
‎06-27-2014 09:38 AM
Super User
Super User
Posts: 7,401

Re: choose unique proc/data steps based on value of variable

Well, a few methods spring to mind.  Macros would be one, code generation another.  For macros:

%macro Do_steps_A();

     ...do some steps;

%mend;

%macro Do_steps_B();

     ... do some steps;

%mend;

data have;

     set have;

     if test="A" then call execute('%do_steps_a;');

     else call execute('%do_steps_b;');

run;

For the code generation its pretty much the same except instead of calling the macro in the call execute you put the actual code.

View solution in original post


All Replies
Solution
‎06-27-2014 09:38 AM
Super User
Super User
Posts: 7,401

Re: choose unique proc/data steps based on value of variable

Well, a few methods spring to mind.  Macros would be one, code generation another.  For macros:

%macro Do_steps_A();

     ...do some steps;

%mend;

%macro Do_steps_B();

     ... do some steps;

%mend;

data have;

     set have;

     if test="A" then call execute('%do_steps_a;');

     else call execute('%do_steps_b;');

run;

For the code generation its pretty much the same except instead of calling the macro in the call execute you put the actual code.

Respected Advisor
Posts: 3,124

Re: choose unique proc/data steps based on value of variable

Or if you have SAS 9.3 M2 or newer, you can use: DOSUBL() routine.

Haikuo

Respected Advisor
Posts: 3,887

Re: choose unique proc/data steps based on value of variable

I probably don't understand your requirement but may be you're after something as simple as:

proc print data=have(where=(variable='value1'));

run;

proc print data=have(where=(variable='value2'));

run;

Trusted Advisor
Posts: 1,204

Re: choose unique proc/data steps based on value of variable

You can do this using macro. Here is an example you can modify this based on your own variable. Dataset "have" 3 variables one of them is gender, processing is being done based on values of gender variable. Hope this helps.

data have;
      input gender $ age :3.1 height weight @@;
      datalines;
f 143 56.3  85.0 f 155 62.3 105.0 f 153 63.3 108.0 f 161 59.0  92.0
f 191 62.5 112.5 f 171 62.5 112.0 f 185 59.0 104.0 f 142 56.5  69.0
f 160 62.0  94.5 f 140 53.8  68.5 f 139 61.5 104.0 f 178 61.5 103.5
f 157 64.5 123.5 f 149 58.3  93.0 f 143 51.3  50.5 f 145 58.8  89.0
m 164 66.5 112.0 m 189 65.0 114.0 m 164 61.5 140.0 m 167 62.0 107.5
m 151 59.3  87.0
;

%macro process(dsname);
proc sql;
select quote(trim(gender)) into :g from &dsname where gender='f';
quit;


%if &g="f" %then %do;

proc means data=&dsname;
var height weight;
run;
%end;

%else %do;

data want;
set &dsname;
ratio=weight/height;
run;
%end;
%mend process;

%process(have)

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 241 views
  • 0 likes
  • 5 in conversation