DATA Step, Macro, Functions and more

How to recursively get the number of observation, looping through data sets

Accepted Solution Solved
Reply
Senior User
Posts: 1
Accepted Solution

How to recursively get the number of observation, looping through data sets

[ Edited ]

Hi everyone,

what I'm trying to achieve is loop through some tables-data sets, getting the number of observation of each table.

So, suppose I’ve the following situation:

 

 

data tabl1;
	input name $ age;
	datalines;
	TOM 40
	TIM 50
	JIM 30
	JOHN 20
	;
run;
data tabl2;
	input surname $ city $;
	datalines;
	JOHNSON LONDON
	THOMPSON ROME
	STONE TORONTO
	;
run;

 

Reading the various support forums, I’ve come across with this solution:

1. create a data set with the tables I want:

data tabs;
	set sashelp.vstable;
	where libname='WORK' and upcase(memname) contains 'TABL';
	tab_name=memname;
	keep tab_name;
run;

2. create an array with to loop in and get the number of observations from the descriptive part of the data set:

 

data _null_;
	set work.tabs nobs=n;
	call symput ('num_tab',n);
	call symput ('tab',"");	

run;

data test;
	array tables[&num_tab] $32767. _temporary_;
	if _N_=1 then do j=1 to number_of_obs;
		set work.tabs nobs=number_of_obs;
		tables[j]=tab_name;
	end;
	else go to end_p;
	do i=1 to dim(tables);
		call symputx (cats('tab'),tables[i]);
		if 0 then set work.&tab nobs=nob;
		n=nob;
		output;
	end;
	keep tab_name n;
end_p: run;

Unfortunately I'm not getting the number of observation for each data set.

What I'm trying to achieve s this:

tab_name	n
TABL1		4
TABL2		3

 What I'm doing wrong? Is this the best way to do it?

 

Thanks for the help!

 

T


Accepted Solutions
Solution
2 weeks ago
Regular Contributor
Posts: 164

Re: How to recursively get the number of observation, looping through data sets

in your case:

DATA _NULL_;
   set sashelp.vtable(keep= libname memname nobs);
   where libname eq 'WORK' and memname in ('TABL1','TABL2');
   put 'W' 'ARNING: number of obs in ' libname memname 'is ' nobs;
RUN;
________________________

- Cheers -

View solution in original post


All Replies
Regular Contributor
Posts: 164

Re: How to recursively get the number of observation, looping through data sets

Posted in reply to tommymene86

Hi you can directly extract the number of observations from the SAS metadata. There is no need to Loop through them.

 

DATA _NULL_;
   set sashelp.vtable(keep= libname memname nobs);
   where libname eq 'SASHELP' and memname eq 'CLASS';
   put 'W' 'ARNING: number of obs in ' libname= memname 'is ' nobs;
RUN;
________________________

- Cheers -

Solution
2 weeks ago
Regular Contributor
Posts: 164

Re: How to recursively get the number of observation, looping through data sets

in your case:

DATA _NULL_;
   set sashelp.vtable(keep= libname memname nobs);
   where libname eq 'WORK' and memname in ('TABL1','TABL2');
   put 'W' 'ARNING: number of obs in ' libname memname 'is ' nobs;
RUN;
________________________

- Cheers -

Super User
Posts: 13,583

Re: How to recursively get the number of observation, looping through data sets


@Oligolas wrote:

in your case:

DATA _NULL_;
   set sashelp.vtable(keep= libname memname nobs);
   where libname eq 'WORK' and memname in ('TABL1','TABL2');
   put 'W' 'ARNING: number of obs in ' libname memname 'is ' nobs;
RUN;

@Oligolas

May I ask why you use

put 'W' 'ARNING: number of obs in '

instead of

put 'WARNING: number of obs in '

?

Respected Advisor
Posts: 3,058

Re: How to recursively get the number of observation, looping through data sets

So if you are searching a SASLOG for WARNING, the line of code that begins put 'W' isn't found by the search.

 

Similarly, you can do this for user generated ERROR statements that appear in the SASLOG.

--
Paige Miller
Regular Contributor
Posts: 164

Re: How to recursively get the number of observation, looping through data sets

oh.. just routine. By writing it this way I bypass a program that searches for warnings, errors and special notes in the log and I directly get to the section where the notification is raised when I search by myself

________________________

- Cheers -

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 82 views
  • 1 like
  • 4 in conversation