BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuser4321
Obsidian | Level 7

 


I have the following code with me. It outputs 3 tables in the "results" section for each data set. Third table in the "results" section has the names of the variables along with other details such as type, length, etc. I need to combine this third table for each of these data sets so that finally, I just have one table which has the names (and type, length, etc.) of all the variables that are there in all of these tables.

Basically, my goal is to merge all the third tables in the "Results" section so as to get all this information in just one table.

Help would be appreciated. Thank you.

libname a meta library="abc" metaout=data;

data list;
     infile datalines4 dlm='|';
    format table $32.;
    input table;
datalines4;
table_a
table_b
table_c
table_d
table_e
table_f
table_g
table_h
;;;;
run;

data _null_;
    set list;
    call execute('
        proc contents data=a.'||strip(table)||';
        run;
    ');
run;
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Sure. See if you can use this as a template. Below, I create 3 data sets for demonstration, table_a, table_b and table_c. I simplify your list data a bit to have those 3 data sets listed. Then I use the dictionary.columns metadata to retrieve information from the tables listed in the list data set. 

 

Obviously, you would have to change the WORK library to A below. 

 

Let me know if it works for you 🙂

 

data table_a; set sashelp.class; run;
data table_b; set sashelp.class; run;
data table_c; set sashelp.class; run;

data list;
     infile datalines4 dlm='|';
    format table $32.;
    input table;
datalines4;
table_a
table_b
table_c
;;;;
run;

proc sql;
   create table want as
   select a.* 
   from dictionary.columns a
      , list b
   where upcase(a.memname) = upcase(b.table)
   and libname = 'WORK'
   ;
quit;

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

All this information is already available in dictionary.columns and sashelp.cvcolumn metadata tables. No need to jump hoops like this.

 

data want;
   set sashelp.vcolumn;
   where libname = 'SASHELP';
run;
SASuser4321
Obsidian | Level 7
I tried something similar but it didn't work for me. What would your code in my case look like exactly? My libname is 'a' and the name of my set is 'list' as is shown in my original post above.
PeterClemmensen
Tourmaline | Level 20

Sure. See if you can use this as a template. Below, I create 3 data sets for demonstration, table_a, table_b and table_c. I simplify your list data a bit to have those 3 data sets listed. Then I use the dictionary.columns metadata to retrieve information from the tables listed in the list data set. 

 

Obviously, you would have to change the WORK library to A below. 

 

Let me know if it works for you 🙂

 

data table_a; set sashelp.class; run;
data table_b; set sashelp.class; run;
data table_c; set sashelp.class; run;

data list;
     infile datalines4 dlm='|';
    format table $32.;
    input table;
datalines4;
table_a
table_b
table_c
;;;;
run;

proc sql;
   create table want as
   select a.* 
   from dictionary.columns a
      , list b
   where upcase(a.memname) = upcase(b.table)
   and libname = 'WORK'
   ;
quit;
SASuser4321
Obsidian | Level 7

@PeterClemmensen , unfortunately it doesn't work for me. Attached with this comment is the picturedoesn'twork.PNG of what my screen shows me. I appreciate your effort and have upvoted your comments but I was wondering if you know another way to do this, preferably following the line of thought in this:

data _null_;
    set list;
    call execute('
        proc contents data=a.'||strip(table)||';
        run;
    ');
run;

Maybe, the code could merge/append as it loops? 

PeterClemmensen
Tourmaline | Level 20

What does not work? 🙂 Do you get an error or simply different results than what you expect? Please be specific.

SASuser4321
Obsidian | Level 7
I edited my above comment and added a picture of what the screen shows me as a result of the code 🙂
PeterClemmensen
Tourmaline | Level 20

Please post the log from the code you ran.

SASuser4321
Obsidian | Level 7
libname a meta library="xyz" metaout=data;
775 data list;
776 infile datalines4 dlm='|';
777 format table $32.;
778 input table;
779 datalines4;

NOTE: The data set WORK.LIST has 28 observations and 1 variables.
NOTE: Compressing data set WORK.LIST increased size by 100.00 percent.
Compressed is 2 pages; un-compressed would require 1 pages.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


808 ;;;;
809 run;
810
811 proc sql;
812 create table want as
813 select a.*
814 from dictionary.columns a
815 , list b
816 where upcase(a.memname) = upcase(b.table)
817 and libname = 'a'
818 ;
NOTE: Table WORK.WANT created, with 0 rows and 18 columns.

819 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


820
821 GOPTIONS NOACCESSIBLE;
822 %LET _CLIENTTASKLABEL=;
823 %LET _CLIENTPROCESSFLOWNAME=;
824 %LET _CLIENTPROJECTPATH=;
825 %LET _CLIENTPROJECTPATHHOST=;
826 %LET _CLIENTPROJECTNAME=;
827 %LET _SASPROGRAMFILE=;
828 %LET _SASPROGRAMFILEHOST=;
829
830 ;*';*";*/;quit;run;
831 ODS _ALL_ CLOSE;
3 The SAS System Thursday, November 17, 2022

832
833
834 QUIT; RUN;
835 %_eg_gridremoteepilogue;
836 %sysrput _EGRCGRID=&_EGRC;
NOTE: Remote submit to GRID complete.
2 %_eg_gridclientepilogue;
3 %let _EGRC=&_EGRCGRID;
4
PeterClemmensen
Tourmaline | Level 20

The 'a' should be a capital 'A' in the code.

mkeintz
PROC Star

If all you care about is to get a list of variables present in at least one of the datasets, then just make an empty dataset of all the component datasets, and run a proc contents:

 

data dummy;
  set a.table_a
      a.table_b
      a.table_c
      a.table_d
      a.table_e
      a.table_f
      a.table_g
      a.table_h ;
  stop;
run;
proc contents data=dummy; 
run;

Note the "stop:" statement tells the data step to stop, even before the first observation is processed.  So no excess input/output is performed. But it will write out the header. So you'll have zero obs, but all the variables.

 

Note this assumes that any common variable will be either numeric in every instance, or character in every instance. 

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

--------------------------

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 1221 views
  • 3 likes
  • 3 in conversation