Hi all,
I'm new to SAS and SAS Macro and I'm having trouble with this. I'm trying to write a macro that will call one macro for categorical data, and another for numerical data. This is what I've tried so far:
%macro please_help(dataset, var1); %if vartype(&dataset, varnum(&dataset, &var1))=C %then %do; %cat_macro(&dataset, &var1); %end; %else %do; %num_macro(&dataset, &var1); %end; %mend;
I'm getting the following error:
ERROR: Required operator not found in expression: vartype(&dataset, varnum(&dataset, &var1))=C
Could anyone suggest a correct way to find what type of data is entered, or maybe a different approach? I have also tried vtype and vtypex instead of vartype, and got the same error.
Thanks
You cannot use sas functions directly within a macro code but under %sysfunc sas macro function.
Try next code:
%macro please_help(dataset, var1);
%let dsid = %sysfunc(open(&dataset));
%if %sysfunc(vartype(&dsid, %sysfunc(varnum(&dsid, &var1)))) = C %then %do;
%cat_macro(&dataset, &var1);
%end;
%else %do;
%num_macro(&dataset, &var1);
%end;
%let dsid = %sysfunc(close(&dsid));
%mend;
Alternativly you can try:
%macro my_run (dataset, var);
data _null_;
dsid = open("&dataset");
if dsid > 0 then do;
if vartype(dsid, varnum(dsid,&var)) = 'C'
then call execute("%cat_macro(&dataset, &var)" );
else call execute("%num_macro(&dataset, &var)" );
dsid = close(dsid);
end;
run;
%mend;
%my_run;
You cannot use sas functions directly within a macro code but under %sysfunc sas macro function.
Try next code:
%macro please_help(dataset, var1);
%let dsid = %sysfunc(open(&dataset));
%if %sysfunc(vartype(&dsid, %sysfunc(varnum(&dsid, &var1)))) = C %then %do;
%cat_macro(&dataset, &var1);
%end;
%else %do;
%num_macro(&dataset, &var1);
%end;
%let dsid = %sysfunc(close(&dsid));
%mend;
Alternativly you can try:
%macro my_run (dataset, var);
data _null_;
dsid = open("&dataset");
if dsid > 0 then do;
if vartype(dsid, varnum(dsid,&var)) = 'C'
then call execute("%cat_macro(&dataset, &var)" );
else call execute("%num_macro(&dataset, &var)" );
dsid = close(dsid);
end;
run;
%mend;
%my_run;
Create a dataset with variable attributes from dictionary.columns, and then use call execute from that:
proc sql;
create table control as select libname,memname,name,type from dictionary.columns where libname = upcase("&lib.") and memname = upcase ("&dataset.") and upcase(name) = upcase("&var1.");
quit;
data _null_;
if type = 'char'
then call execute(cats('%cat_macro(',libname,'.',memname,',',name,');'));
else call execute(cats('%num_macro(',libname,'.',memname,',',name,');'));
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.