BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

Hello,

 

I am trying to get specified variables from all data sets in a library. The code shown is just for one data set. I wonder if this can be automated using CALL EXECUTE or by other means for all data sets.

 

proc sql noprint;
	select name into: list_var separated by ', '
	from dictionary.columns
	where upcase(libname) = 'X' and upcase(memname) = 'Y' and (strip(upcase(name)) like '%DAY' or strip(upcase(name)) like '%MON' or strip(upcase(name)) like '%YR');
quit;

%put &list_var;

proc sql noprint;

	create table t1 as
	select &list_var
		from X.Y
	;
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You will need to make some decisions about what code you want to create.  

First get the data.

proc sql noprint;
create table varlist as
  select libname,memname,varnum,name
  from dictionary.columns
  where libname=%upcase('X')
   and (upcase(name) like '%DAY' 
     or upcase(name) like '%MON'
     or upcase(name) like '%YR'
       )
  order by libname,memname,varnum
;
quit;

Then use the data to generate code. For example here is code to make the subsets into WORK library with the same member name as the dataset had in the X library.

filename code temp;
data _null_;
  file code;
  set varlist;
  by memname;
  if first.memname then put
    'data work.' memname ';'
  / '  set ' libname +(-1) '.' memname '(keep=' @
  ;
  put name @;
  if last.memname then put 
    ');'
  / 'run;'
  ;
run;

Then run the code

%include code / source2;

 

View solution in original post

6 REPLIES 6
Reeza
Super User

I wonder if this can be automated using CALL EXECUTE or by other means for all data sets.

Yes, it can be automated.

 

Use SASHELP.VTABLE which as the list of tables in each library similar to dictionary.columns. 

 

Personally I would recommend using PROC APPEND with a KEEP statement instead. This copies the data set and doesn't do it line by line so would be the fastest approach IMO. 

 

 

proc append base=newName data=oldName (keep = &list_var.);
run;

 

FYI - if you had used a naming convention that relied on prefixes rather than suffixes this would be an incredibly trivial solution since you could use short cuts. 

 

Here's a tutorial on doing this with macros. The last step of using CALL EXECUTE shows how to make it data driven.

 

https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

SAS_inquisitive
Lapis Lazuli | Level 10
@Reeza. I was trying to create individuals data sets containing only specified variables for respective data sets in a library.
Reeza
Super User

@SAS_inquisitive wrote:
@Reeza. I was trying to create individuals data sets containing only specified variables for respective data sets in a library.

That is what this code does, did you run the code and try it?

 

Then generating that code dynamically from a data step is trivial. 

 

 

SAS_inquisitive
Lapis Lazuli | Level 10
@Reeza, I will try that as well. Thanks !
Tom
Super User Tom
Super User

You will need to make some decisions about what code you want to create.  

First get the data.

proc sql noprint;
create table varlist as
  select libname,memname,varnum,name
  from dictionary.columns
  where libname=%upcase('X')
   and (upcase(name) like '%DAY' 
     or upcase(name) like '%MON'
     or upcase(name) like '%YR'
       )
  order by libname,memname,varnum
;
quit;

Then use the data to generate code. For example here is code to make the subsets into WORK library with the same member name as the dataset had in the X library.

filename code temp;
data _null_;
  file code;
  set varlist;
  by memname;
  if first.memname then put
    'data work.' memname ';'
  / '  set ' libname +(-1) '.' memname '(keep=' @
  ;
  put name @;
  if last.memname then put 
    ');'
  / 'run;'
  ;
run;

Then run the code

%include code / source2;

 

SAS_inquisitive
Lapis Lazuli | Level 10
Works great. Thanks, @Tom.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 725 views
  • 2 likes
  • 3 in conversation