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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.