BookmarkSubscribeRSS Feed
arsn14
Calcite | Level 5

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

6 REPLIES 6
arsn14
Calcite | Level 5

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

Ron_MacroMaven
Lapis Lazuli | Level 10

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)

arsn14
Calcite | Level 5

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?

andreas_lds
Jade | Level 19

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;

Ron_MacroMaven
Lapis Lazuli | Level 10

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)

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
  • 6 replies
  • 2060 views
  • 1 like
  • 4 in conversation