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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Oligolas
Barite | Level 11

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

5 REPLIES 5
Oligolas
Barite | Level 11

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 -

Oligolas
Barite | Level 11

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 -

ballardw
Super User

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

?

PaigeMiller
Diamond | Level 26

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
Oligolas
Barite | Level 11

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 -

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 959 views
  • 1 like
  • 4 in conversation