DATA Step, Macro, Functions and more

Can we get count of rows in a macro variable for a blank dataset

Reply
Occasional Contributor
Posts: 12

Can we get count of rows in a macro variable for a blank dataset

How to count the rows of dataset and assign it macro variable?if the dataset is blank then it has to have macro variable value as 0 .kindly help.
Super Contributor
Posts: 340

Re: Can we get count of rows in a macro variable for a blank dataset

[ Edited ]

Hi,

 

The following contains a solution to the same question:

 

https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-count-of-total-records-in-a-table/td-...

 

Edit: You can assign the macro variable a value of 0 before trying to find the actual value.

 

Regards,

Amir.

 

 

 

Valued Guide
Posts: 585

Re: Can we get count of rows in a macro variable for a blank dataset

untested:

%let count = 0;
proc sql noprint;
  select nobs into :count trimmed
    from sashelp.vtable
      where libname = "LIB" and memname = "DATASET"
  ;
quit;

Note that library and dataset-name have to be all upcase.

Super User
Super User
Posts: 9,617

Re: Can we get count of rows in a macro variable for a blank dataset

Why do you need it in a macro variable?  The reason I ask is is because there are better methods to run code if observations exist, and in most scenarios code will run even if there are no observations.  So there is rarely or never a need to create a macro variable for this, especially since the data is already present in sashelp.vtable, e.g.

data _null_;
  set sashelp.vtable (where=(libname="<yourlib>" and memname="<yourds>"));
  call symputx('num_obs',nobs);
run;

%put &num_obs.;

 

Respected Advisor
Posts: 4,743

Re: Can we get count of rows in a macro variable for a blank dataset

@Sharathr

For the discussion @Amir references I'd write the macro a bit differently as below:

%macro get_table_size(inset,macvar);
  %global &macvar;
  data _null_;
    call symput("&macvar",put(nobs,f15. -l));
    stop;
    set &inset nobs=nobs;
  run;
%mend;



%get_table_size(sashelp.class,n_obs)
%put &=n_obs;

data test;
  length var 8;
  stop;
run;
%get_table_size(test,n_obs)
%put &=n_obs;
Ask a Question
Discussion stats
  • 4 replies
  • 69 views
  • 0 likes
  • 5 in conversation