Debug my code

Reply
Regular Contributor
Posts: 190

Debug my code

plz find & remove the

 

 

%let dsName=;
57 options nomprint nomlogic;
58
59 %macro check(dsName= );
60
61 %let dsid = %sysfunc(open(&dsName, IS));
62
63 %if &dsid = 0 %then %put %sysfunc(sysmsg());
64
65 %let nlobs = %sysfunc(attrn(&dsid,NLOBS));
66
67 %put No of Observation in dataset %trim(&dsName) : %trim(&nlobs);
68
69 %let rc = %sysfunc(close(&dsid));
70
71 %mend;
72
73 %check(dsName=);
ERROR: There is not a default input data set (_LAST_ is _NULL_).
WARNING: Argument 1 to function ATTRN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set
to a missing value.
Super User
Super User
Posts: 7,432

Re: Debug my code

You have not provided a dataset in the parameter to the macro call:

%check(dsName=);

                            ^

This is invalid as open needs a valid dataset.  As you have gone ahead with this solution, you will need to include further code at the beginning of the macro to ensure that the dataset is valid and exists, i.e.

%if %sysfunc(exists(&dsname.)) %then %do;

  your macro code

%end;

%else %do;

  %put Dataset &dsname. does not exist;

%end;

 

Or, your could just not use macro code at all and go with:

proc sql noprint;
  select  NOBS 
  into    NUMOBS
  from    DICTIONARY.TABLES 
  where   LIBNAME="SASHELP"
    and   MEMNAME="CLASS";
quit;

As there is almost never a "need" to do things in macro language.

 

 

 

 

Regular Contributor
Posts: 190

Re: Debug my code

%let dsName=;
options nomprint nomlogic;

%macro check(dsName= );

%let dsid=%sysfunc(open(&dsName, IS));

%if &dsid=0 %then %put %sysfunc(sysmsg());

%let nlobs= %sysfunc(attrn(&dsid,NLOBS));

%put No of Observation in dataset %trim(&dsName) : %trim(&nlobs);

%let rc= %sysfunc(close(&dsid));

%mend;

%check(dsName=)

add ?? where?? show..
Super User
Super User
Posts: 7,432

Re: Debug my code

Here, in my post, where the upward point triangle is, you need to put a dataset (with library if not work);

%check(dsName=);

                            ^

 

E.g. 

%check(dsName=sashelp.class);

 

                            

Regular Contributor
Posts: 190

Re: Debug my code

basically i need a macro to display number of observations in a dataset for eg dsname=100
Regular Contributor
Posts: 190

Re: Debug my code

can u rectify my cod e& give me -thanks
Regular Contributor
Posts: 190

Re: Debug my code

%let dsName=;
57 options nomprint nomlogic;
58
59 %macro check(dsName= );
60
61 %let dsid=%sysfunc(open(&dsName, IS));
62
63 %if &dsid=0 %then %put %sysfunc(sysmsg());
64
65 %let nlobs= %sysfunc(attrn(&dsid,NLOBS));
66
67 %put No of Observation in dataset %trim(&dsName) : %trim(&nlobs);
68
69 %let rc= %sysfunc(close(&dsid));
70
71 %mend;
72
73 %check(dsName=work.data1);
ERROR: File WORK.DATA1.DATA does not exist.
WARNING: Argument 1 to function ATTRN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set
to a missing value.
No of Observation in dataset work.data1 : . getting this error what to do??
Super User
Super User
Posts: 7,432

Re: Debug my code

In your library WORK, there is no dataset called DATA1.  This is what the log message is telling you.  The macro will only work on a dataset which exists in the library given.  So open SAS, find a dataset, if there is nothing in your WORK library, then look at SASHELP.  Take the CLASS dataset as an example from there and replace work.data1 in your example with sashelp.class.

Occasional Contributor
Posts: 7

Re: Debug my code

[ Edited ]

If you need a macro that gives you the number of rows in a table, the minimum input to this macro needs to be the name of a specific table for which you want the number of rows.

E.g. 

%check(dsName=sashelp.class);

You suggest a solution where you would like to to supply "dsName=100". What kind of input is the value 100? How will this input be used to give you the number of rows in a table?

 

Perhaps you are not yet familiar with macro functionality and how to call a macro? Then I suggest some reading. E.g.

Introduction to the Macro Facility

Trusted Advisor
Posts: 1,115

Re: Debug my code

[ Edited ]

Hi @RTelang,

 

If you just "need a macro to display number of observations in a dataset" (as you say) or a macro for some other day-to-day task, you can resort to existing macros on the web. Why reinvent the wheel? For example, have a look at Roland's SAS® Macros. This website also contains many tips and tricks for writing your own macros from the author of this large macro collection, Roland Rashleigh-Berry (a former colleague of mine).

Ask a Question
Discussion stats
  • 9 replies
  • 431 views
  • 1 like
  • 4 in conversation