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
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 -
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 -
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 -
@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;
May I ask why you use
put 'W' 'ARNING: number of obs in '
instead of
put 'WARNING: number of obs in '
?
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.
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 -
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.