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

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
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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);

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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.

FreelanceReinh
Jade | Level 19

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);
r_behata
Barite | Level 11

@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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1172 views
  • 2 likes
  • 3 in conversation