DATA Step, Macro, Functions and more

Combining two macros using a %do loop with a %if-%then-%do-%else-%do-%end loop

Reply
Occasional Contributor
Posts: 5

Combining two macros using a %do loop with a %if-%then-%do-%else-%do-%end loop

So I have two macros that I need to combine, I've been eyeing this thing for a while and can't seem to get it to work. It seems to be a simple step but I can't seem to get it recognize the new macro I create "Do_Freq_Req". The goal is to have the new macro "Do_Freq_Reg" execute as often as the number of variable in the analysis, and to have it branch into the Do_Freq and Do_Req macros that already exists. Below is the code of the two individual Do_Freq and Do_Reg macros.

COMBINE THE NEXT TWO MACROS INTO ONE MACRO

%Macro Do_Freq(Input, Title);

Proc Freq Data= Alldel;

Tables &Input;

Title "Variable Summarized: &Title";

Run;

%Mend Do_Freq;

%Do_Freq(Hyperten, Hypertension during pregnancy?);

%Do_Freq(Diabetes, Prepregnancy diabetes?);

%Do_Freq(Gestatio, Gestational diabetes?);  

%Do_Freq(Drug_Abu, Drugs use during pregnancy?);    

%Do_Freq(Smoke, Smoker?);    

%Do_Freq(HIV, HIV?);

%Do_Freq(Parity, Parity);

%MACRO DO_REG(Predict);

PROC REG DATA=ALLDEL;

&PREDICT: MODEL FETAL_WE=&PREDICT;

RUN;

QUIT;

%MEND DO_REG;

%DO_Reg(IndRace);

%Do_Reg(IndSmoke);

%Do_Reg(DeltaBmi)

%Do_Reg(Weeks_ge)

%Do_Reg(Age);

Any help is appreciated.

Thanks

Super User
Posts: 5,080

Re: Combining two macros using a %do loop with a %if-%then-%do-%else-%do-%end loop

Occasional Contributor
Posts: 5

Re: Combining two macros using a %do loop with a %if-%then-%do-%else-%do-%end loop

Astounding,

The link shows me how to do a loop, but I was wondering how do I join two different MACROS as listed above, into a single new MACRO with a do loop and a if and then statement included that would branch into the two individual MACROS

Regular Contributor
Posts: 198

Re: Combining two macros using a %do loop with a %if-%then-%do-%else-%do-%end loop

I suggest that you stick with your two macros.

Each is independent and therefore easy to test.

Then what you need is a data set that has the list of criteria for calling each macro.

for your frequency macro,

that looks like variable name and label.

Here is the tool to call the macro using a data set as list of parameters.

http://www.sascommunity.org/wiki/Macro_CallMacr

For this tool to work

your list, control data set, has to have the variables the same as the macro.

I recommend that you use proc contents output data since that has (variable) name and label

PROC contents data = &libname..&memname

                     noprint

              out  = out_contents

             (keep = memname name length

                     type    length  format

                     nobs    varnum  label

              rename = (nobs = _n_obs

                        type = _type_n));

My style guide says: "always use named parameters in macros"

so this is my recommendation for your parameter names

%Macro Do_Freq(name=, label=);

*testing;

%callmacro(data = out_contents(keep = name label))

*auto-magic;

%callmacro(data = out_contents(keep = name label)

         ,macro_name=do_freq)

Occasional Contributor
Posts: 5

Re: Combining two macros using a %do loop with a %if-%then-%do-%else-%do-%end loop

Ron

Thank you for your reply, but I was wondering if I was to run a new macro with a 'if then statement' to call upon the stated Macros, how would I start that?

Super Contributor
Posts: 259

Re: Combining two macros using a %do loop with a %if-%then-%do-%else-%do-%end loop

Still hardly possible to know what you want to achieve by combining those two macros, because they have nothing in common except for the dataset used.

/* Untested code ahead! */

%macro Container(Input=, Title=, Predict=);

  %if %length(&Input) > 0 and %length(&Title) > 0 %then do;

    %Do_Freq(&Input, &Title);

  %end;

  %if %length(&Predict) > 0 %then %do;

    %DO_REG(Predict);

  %end;

%mend;

Regular Contributor
Posts: 198

Re: Combining two macros using a %do loop with a %if-%then-%do-%else-%do-%end loop

I agree with andreas_Ids solution

your two macro sub-routines do not appear to have the same parameters

I am not a user of proc req

so I will not guess that your parameter named 'predict' is a variable name

if parameter 'predict' is a variable name then consider this

%macro choose_verb(data = sashelp.class

,list_names = a b c d e f h);

%let n_items = %sysfunc(countw(&list_names));

%put echo &=n_items;

%do i = 1 %to &n_items;

   %let item = %scan(&list_names,&i);

   %put echo &=item;

   %*automagic: get type of variable;

   %*let dsid = %sysfunc(open());

   %*let var_num = %sysfunc(varnum());

   %*let type = %sysfunc(vartype());

   %*let rc = %sysfunc(close());

   %let type = c;%*<---<<<;

   %if &type eq c %then %put do_freq(data= &data,name = &item);

   %else %put do_reg(data= &data,name = &item);

   %end;

%mend;

%choose_verb(data = work.all_del

,list_names = age sex height weight)

Ask a Question
Discussion stats
  • 6 replies
  • 466 views
  • 1 like
  • 4 in conversation