BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
borovy
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Haikuo
Onyx | Level 15

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

Haikuo

Patrick
Opal | Level 21

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;

stat_sas
Ammonite | Level 13

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)

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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