BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sean100
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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;

 

 

View solution in original post

3 REPLIES 3
Shmuel
Garnet | Level 18

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;

 

 

Sean100
Calcite | Level 5
Thanks a million, I tried the first method and it worked perfectly!
Kurt_Bremser
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 820 views
  • 2 likes
  • 3 in conversation