BookmarkSubscribeRSS Feed
jsem
Calcite | Level 5

Hi, I'm using SAS 9.4 and I'd like to use the above code but filter out (in some way) a specific list of SUBNUM from each table.  i.e. the SUBNUM list (below, right now about 8, but up to about 15) are the only SUBNUM I want in each tab, but I'd like all records for each SUBNUM in the list.  The SUBNUM variable is in every dataset.  Not all datasets will have records for each SUBNUM from the list, and some tables will have more than one record per SUBNUM from the list.  Thank you so much for your help.

 

libname ABC 'X:\Folder\SASData';

data _null_;
     call symputx('datestamp', put(date(), YYMMDDN.));
run;

libname XL xlsx "%sysfunc(pathname(WORK)) \..\..\..\..\..\Filename Output &datestamp..xlsx";
run;

proc copy inlib=ABC outlib=XL mt=data; 
run;

 

SUBNUM
123
345

 

 

Example data from one dataset that I want to filter for SUBNUM '123' and '345':

Column1SUBNUMColumn2Column3
data123datadata
data345datadata
data789datadata
data111datadata
data123datadata
data789datadata
data123datadata
4 REPLIES 4
Tom
Super User Tom
Super User

Just use a DATA step instead of PROC COPY.  

data XL.dataset1 ;
  set ABC.dataset1 ;
  where subnum in (123 456);
run;

You will just need to use some code generation to create a separate data step for each member in ABC.

proc contents data=abc._all_ noprint out=contents; run;
filename code temp;
data _null_;
  set contents ;
  by memname;
  if first.memname then subnum=0;
  if lowcase(name)='subnum' then subnum=1;
  if last.memname;
  file code ;
  put 'data xl.' memname '; set abc.' memname ';' ;
  if subnum then put 'where subnum in (123 456);' ;
  put 'run;';
run;
%include code / source2;

 

jsem
Calcite | Level 5

Hi Tom, thanks so much!  Do I need to change anything in the code besides dataset1 and 123 456?  e.g. is memname = dataset1?  Just checking 🙂  Thanks again!

Jenn

Tom
Super User Tom
Super User

If the list of SUBNUM value is short enough then put it into a macro varaible.

%let subnum_list=123 456 ;
...
where subnum in (&subnum_list)
...

If the list of SUBNUM values is large then keep it in a dataset and generate different code. 

For example you could copy one dataset with SQL code like this:

create table XL.dataset1 as 
  select a.* 
  from ABC.dataset1 a 
  where a.subnum in (select b.subnum from SUBNUM_LIST b )
;
jsem
Calcite | Level 5

Hi Tom, thanks so much!  The SUBNUM list isn't very long (less than 15 unique), it's the dataset list that's long.  There's 85 datasets and I'm concerned that having this code for 85 datasets would be a strain.  Also, it took me a bit but I figured out that my data thinks the SUBNUM are char (oops).  Thanks for your continued support!

data XL.dataset1 ;
  set ABC.dataset1 ;
  where subnum in (123 456);
run;

   

Jenn

 

PS I loved the idea for the let subnum_list, it worked like a charm! 

%let subnum_list=123 456 ;

 

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 510 views
  • 0 likes
  • 2 in conversation