BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
spirto
Obsidian | Level 7

Hello I have the following dataset format:

R1R2R3R4R5
ABCDE
FGHIJ
KLMNO
PQRST
...............

and I would like to create 4 new datasets photo1, photo2, photo3, photo4, etc:

R1R2R3R4R5
ABCDE

R1R2R3R4R5
FGHIJ

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

5 REPLIES 5
Reeza
Super User

There's many ways, but I have to ask why, there's almost always a better way to deal with this situation.

Split Data into Subsets - sasCommunity

Reeza
Super User

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;

stat_sas
Ammonite | Level 13

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

spirto
Obsidian | Level 7

Thank you both!

Ksharp
Super User

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-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
  • 5 replies
  • 2210 views
  • 6 likes
  • 4 in conversation