BookmarkSubscribeRSS Feed
noda6003
Quartz | Level 8

I have few datasets and i am splitting them to different folders based on conditions. The problem is if there is one folder where there is no condition satisfied then it just creates a dataset with 0 observations. Can i remove them or have some condition for not creating them if condition is not satisfied.?

 

I am using the belwo code:

proc datasets nolist;
	copy in=analysis out=work;
quit;
run;

%*split the data into different folders based on fold;
data _null_;
  set sashelp.vtable (where=(libname="WORK"));
  call execute('data sa.'||strip(memname)||'; set WORK.'||strip(memname)||'; where fold="19A"; run;');
  call execute('data s1a.'||strip(memname)||'; set WORK.'||strip(memname)||'; where fold="10A"; run;');
  call execute('data s2a.'||strip(memname)||'; set WORK.'||strip(memname)||'; where fold="12A"; run;');
run;
3 REPLIES 3
MichaelLarsen
SAS Employee

A cleanup after copying of the tables.

 

/* Remove empty tables */
data _null_;
  set sashelp.vtable (where=(libname in ("SA","S1A","S2A") and nobs=0));
  call execute(catt('proc delete data=',Libname,'.',Memname,';run;'));
run;
Shmuel
Garnet | Level 18

You can split your data within one step per dataset:

 

%macro split(memname);
    data sa.&memname
           s1a.&memname
           s2a.&memname
     ;
      set work.&memname;
          if folder = '19A' then output sa.&memname; else
          if folder = '10A' then output s1a.&memname; else
          if folder = '12A' then output s2a.&memname;
          else delete;
   run;
%mend split;
%split(<mem1>);
%split(<mem2>);
...    

thus will save I/O - each observation you'll read only once and write it to output.

MichaelLarsen
SAS Employee

I agree with @Shmuel performance might be an issue, so avoid I/O as much as possible.

 

Instead of creating a macro, I have made an example that writes the data step code that does the copying and then include that code afterwards to execute the code.

 

filename pgm temp;
%let copylib=ANALYSIS;
data _null_;
  set sashelp.vtable;
  where Libname = "&Copylib" and nobs > 0;
  file pgm;
  put 'data sa.' memname
    / '     s1a.' memname
    / '     s2a.' memname ';' 
    / '  set ' "&Copylib.." memname ';'
    / '  if fold = "19A" then output sa.' memname ';'
    / '  else if fold = "10A" then output s1a.' memname ';'
    / '  else if fold = "12A" then output s2a.' memname ';'
    / 'run;'
    ;
run;
%inc pgm / Source2;
filename pgm clear;

I created the COPYLIB macro variable to be able to test it on another library.

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