06-04-2015 04:10 PM
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;
Title "Variable Summarized: &Title";
%Do_Freq(Hyperten, Hypertension during pregnancy?);
%Do_Freq(Diabetes, Prepregnancy diabetes?);
%Do_Freq(Gestatio, Gestational diabetes?);
%Do_Freq(Drug_Abu, Drugs use during pregnancy?);
PROC REG DATA=ALLDEL;
&PREDICT: MODEL FETAL_WE=&PREDICT;
Any help is appreciated.
06-04-2015 05:14 PM
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
06-04-2015 08:56 PM
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.
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
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=);
%callmacro(data = out_contents(keep = name label))
%callmacro(data = out_contents(keep = name label)
06-04-2015 09:28 PM
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?
06-05-2015 03:22 AM
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;
%if %length(&Predict) > 0 %then %do;
06-08-2015 11:36 AM
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);
%choose_verb(data = work.all_del
,list_names = age sex height weight)