Hello I have the following dataset format:
R1 | R2 | R3 | R4 | R5 |
---|---|---|---|---|
A | B | C | D | E |
F | G | H | I | J |
K | L | M | N | O |
P | Q | R | S | T |
... | ... | ... | ... | ... |
and I would like to create 4 new datasets photo1, photo2, photo3, photo4, etc:
R1 | R2 | R3 | R4 | R5 |
---|---|---|---|---|
A | B | C | D | E |
R1 | R2 | R3 | R4 | R5 |
---|---|---|---|---|
F | G | H | I | J |
etc. I am not sure if this can be done using a BY statement some how or maybe through a proc append, etc. Any suggestions?
Here's an example that splits the CLASS file into one row per file.
It assumes that each file is for each name. Your exact code will vary by how you want to split the file.
data _null_;
set sashelp.class;
by name notsorted;
if first.name then do;
call execute("data row"||put(_n_,z2.)||"; set sashelp.class(where=(name='"||name||"'));run;");
end;
run;
There's many ways, but I have to ask why, there's almost always a better way to deal with this situation.
Here's an example that splits the CLASS file into one row per file.
It assumes that each file is for each name. Your exact code will vary by how you want to split the file.
data _null_;
set sashelp.class;
by name notsorted;
if first.name then do;
call execute("data row"||put(_n_,z2.)||"; set sashelp.class(where=(name='"||name||"'));run;");
end;
run;
data have;
input R1 $ R2 $ R3 $ R4 $ R5 $;
CNT+1;
datalines;
A B C D E
F G H I J
K L M N O
P Q R S T
;
proc sql;
select max(cnt) into :cnt from have;
quit;
%macro sets;
data
%do i=1 %to &cnt;
photo&i
%end;
;
set have(drop=cnt);
if _n_=1 then output photo1;
%do i = 2 %to &cnt;
else if _n_=&i then output photo&i;
%end;
run;
%mend sets;
%sets
Thank you both!
The fastest way is using Hash Table.
data _null_; if _n_ eq 1 then do; if 0 then set sashelp.class; declare hash h(); h.definekey('n'); h.definedata('name','age','weight','height'); h.definedone(); end; set sashelp.class; n+1;h.add();h.output(dataset: cats('_',n)); h.clear(); run;
Xia Keshan
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.