BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HeatherNewton
Quartz | Level 8
How can i get the no of rows in table A and output it as an input of a variable in another table?
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

More than one way, as usual. 

https://www.listendata.com/2017/04/number-of-observations-in-sas-data.html

 

Probably the simplest to understand - note the table names are upper case.

 

proc sql noprint;
 select nobs into :totobs separated by ' ' from dictionary.tables
 where libname='SASHELP' and memname='CARS';
quit;
%put total records = &totobs.;

 

Use the &totobs value in the data step as needed.

 

data want;
x=&totobs;
run;

View solution in original post

10 REPLIES 10
Reeza
Super User

More than one way, as usual. 

https://www.listendata.com/2017/04/number-of-observations-in-sas-data.html

 

Probably the simplest to understand - note the table names are upper case.

 

proc sql noprint;
 select nobs into :totobs separated by ' ' from dictionary.tables
 where libname='SASHELP' and memname='CARS';
quit;
%put total records = &totobs.;

 

Use the &totobs value in the data step as needed.

 

data want;
x=&totobs;
run;
HeatherNewton
Quartz | Level 8

why you dont need dot here after &totobs

data want;

x=&totobs;

run;

 

Kurt_Bremser
Super User

If the macro variable name is not part of a longer string which might be a SAS name, the dot is not needed, but it is considered good practice (especially for beginners!) to always terminate a macro reference with a dot.

Dot-terminated strings will also have distinct coloring in the Enhanced Editor.

See Maxim 48.

HeatherNewton
Quartz | Level 8
I tried but it is empty in table want, what could have gone wrong..
Reeza
Super User
Post your log please.
Kurt_Bremser
Super User

Query the DICTIONARY tables:

proc sql noprint;
select nobs into :number
from dictionary.tables
where libname = "WORK" and memname = "A";
quit;

%put number of obs = &number.;
mkeintz
PROC Star

This is a good situation to use the "IF 0 THEN SET .... NOBS=...;" statement, as in

 

data want;
  if 0 then set table_a (drop=_all_) nobs=number_of_drivers;
  set sashelp.cars (keep=make model msrp);
  perdriver_cost=msrp/number_of_drivers;
run;

Three points:

  1. Even though IF 0 is never true (so the "then SET" is never executed), the value for nobs (stored in number_of_drivers) is established prior to actual data processing.  So it's there for usage.

  2. Don't forget the "drop=_ALL_" dataset name parameter for table_a - that will eliminate columns of missing values for variables exclusive to table_a.

  3. The variable in the "nobs=" argument is not a permanent variable.  If you want it to be permanent, then modify the "if 0" to something like
        if 0 then ...  nobs=xxx;
    followed by 
      number_of_drivers=xxx;
    xxx will not be kept, but number_of_drivers will be.

A nice feature of this approach is that it doesn't require using a separate procedure to get the number of observations, nor the creation of macrovars, or the use of special functions in the data step.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
/*
I prefer this code.
*/

%let dsid=%sysfunc(open(sashelp.class));
%let nobs=%sysfunc(attrn(&dsid.,nlobs));
%let dsid=%sysfunc(close(&dsid.));

%put sashelp.class has &nobs records.;
HeatherNewton
Quartz | Level 8
I tried this it works abd display answer in log
But how to store the answer in a dataset?
Reeza
Super User
%let dsid=%sysfunc(open(sashelp.class));
%let nobs=%sysfunc(attrn(&dsid.,nlobs));
%let dsid=%sysfunc(close(&dsid.));

%put sashelp.class has &nobs records.;

data num_records;
num_obs = &nobs;
run;

In the solution I posted, the table names need to be capitalized.

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
  • 10 replies
  • 1091 views
  • 6 likes
  • 5 in conversation