Useful VTABLE in SAS

Hidden Gems of SAS - 1

SAS is such a powerful software and sometimes it surprises me a lot. It has occurred quite a times with me that I have written a long code or a macro for some task, but later got to know that using one of the hidden gems, the task can be completed quite easily. At Ask Analytics, we will try to unearth many of such gems sooner or later !!!


Have your curiosity ever made you peep into one of the default libraries of SAS : SASHELP.

I often get messages asking for a sample data, well, there are multiple datasets in SASHELP library that can be used for doing practicals in SAS.

Example :  SASHELP.Shoes, SASHELP.Class, SASHELP.Air ...and many more.

Their in a corner I found a glittering table, which I found to be very useful.
 ( Disclaimer : just exaggerating it, please don't expect a glitter)

I am referring to Vtable, which bears a normal SAS dataset icon with a small gear. The table contains vital information of all the datasets in SAS,  both inbuilt datasets and the datasets that you make during your SAS Session.


Suppose You make a dataset in SAS in a library "a":

Libname a "G:\AA\SAS gems";

Data a.Just_like_that;
x = 10;
y = 20;
Run;


Open the VTABLE now and see that that the info about the table is registered in it.


click to enlarge


Now let me now demonstrate utility of the VTABLE:

Case 1:

Suppose I have a library "x" in which I have "N" Datasets of similar type. I need to append all these ... How to go about it. 

/* Let's first create dummy datasets for the case*/
libname X "G:\AA\SAS gems\X";

Data x.data_1;
input a b c;
cards;
10 20 30
40 50 60
;
run;

%macro make_data_quick(n);
data x.Data_&n.;
set  x.Data_1;
a = a * &n.;
b = b + &n.;
c = c - &n.;
run;
%Mend;
%make_data_quick(2);
%make_data_quick(3);
%make_data_quick(4);
%make_data_quick(5);

/* Let me try to solve the appending question as I would have done it earlier*/

/* Well, I would have appended the datasets in either of following ways:*/

/* Solution 1: Had there been only 5 datasets*/

Data X.Final_1;
set Data_1 Data_2 Data_3 Data_4 Data_5;
Run;

/* Solution 2: Had there been more of such datasets*/


%Macro Append;
Data X.Final_2;
set %do i = 1 %to 5;  /*  just change the number here to 1000 is there are 1000 datasets*/
Data_&i.
%end;;
Run;
%Mend;
%Append;

/* 
Both above solution are fine but what if there are 1000 datsets, and to add the complication, the names are not in sequence e.g. names are asdf, chvjb, njkfabkfda, fkjrsbfkja, fnajk, .....

Now what ??? Well, here comes the VTABLE to your rescue.
*/

/* Solution 3:  using VTABLE*/

In VTABLE the names so datasets are stored in MEMNAME column and that of libraries are stored in column LIBNAME, here is the trick :

/* deleting the results of above two methods*/
proc delete data = x.Final_1 x.Final_2; run;

/* Store the names of the dataset of library X into a macro variable*/
Proc SQL;
select memname into :data_list separated by " "
from SASHELP.VTABLE 
where upcase(libname) = "X";
Quit;
/* Voila, it's done*/
Data X.Final_3;
set &data_list.;
Run;


Case_2 :  ......................

Now, I would like to invote you to write next case and send us using "Comment Section" or on our FB page : https://www.facebook.com/askanalytics

The best cases, we find, would be published at Ask Analytics with the names of contributors.

All right then ...



Humble appeal


Download our Android app 

Enjoy reading our other articles and stay tuned with us.

Kindly do provide your feedback in the 'Comments' Section and share as much as possible.


No comments:

Post a Comment

Do provide us your feedback, it would help us serve your better.