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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.