I have a list of sas dataset names (absolute paths) stored in a sas dataset. All I am trying to get the number of observations for each of the dataset by passing the name of the dataset as an argument to a custom function created using sas fcmp.
While below is not the actual code, it is somewhat similar to this :
%macro getobs;
%let id=%sysfunc(open(&dsn));
%let no=%sysfunc(attrn(&id.,nobs));
%let rc=%sysfunc(close(&id.));
%mend;
proc fcmp outlib = work.functions.func;
function no_of_obs(dsn);
no=run_macro('getobs',dsn);
return (no);
endsub;
run;
options cmplib=work.functions;
data _null_;
rc=no_of_obs("sashelp.class");
put rc=;
run;
I suspect there is a timing issue with the macro involved , but cannot wrap my head around it. Any help is appreciated.
Here is the log :
29
30 options cmplib=work.functions;
31
32 data _null_;
33 rc=no_of_obs("sashelp.class");
34 put rc=;
35 run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
2 The SAS System 09:36 Friday, November 5, 2021
33:15
NOTE: Invalid numeric data, 'sashelp.class' , at line 33 column 15.
1 The SAS System 10:38 Friday, November 5, 2021
WARNING: Argument 1 to function ATTRN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
rc=0
rc=0 _ERROR_=1 _N_=1
Hello @r_behata,
In addition to the missing $ sign in the definition of the function, as pointed out by Peter, I think you need to change the first %LET statement in the macro to
%let id=%sysfunc(open(%sysfunc(dequote(&dsn))));
in order to temporarily remove the quotes around the function argument.
And the RUN_MACRO function should have a variable name as the third argument that corresponds to the macro variable which contains the value to be returned:
rc=run_macro('getobs',dsn,no);
The first thing you should repair is that the function expects a numeric input. Put a $ after dsn to make the function accept a character input.
Hello @r_behata,
In addition to the missing $ sign in the definition of the function, as pointed out by Peter, I think you need to change the first %LET statement in the macro to
%let id=%sysfunc(open(%sysfunc(dequote(&dsn))));
in order to temporarily remove the quotes around the function argument.
And the RUN_MACRO function should have a variable name as the third argument that corresponds to the macro variable which contains the value to be returned:
rc=run_macro('getobs',dsn,no);
@FreelanceReinh @PeterClemmensen You guys are awesome! This has worked brilliantly. Can't thank you enough.
Didn't knew that we could nest the %sysfunc in this manner.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.