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;
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;
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.
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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.