DATA Step, Macro, Functions and more

Create multiple datasets from single dataset

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 85
Accepted Solution

Create multiple datasets from single dataset

Hi All,

My data looks like this

NameContents
RE_OPSydney
RE_OPBrussels
RE_OPParis
RE_TRFNewyork
RE_TRFWashington
RE_TRFHouston
RE_TRFDayton

And I want to create 2 multiple datasets of name

Data set RE_OP should contain

NameContents
RE_OPSydney
RE_OPBrussels
RE_OP

Paris

and Dataset RE_TRF should contain

NameContents
RE_TRFNewyork
RE_TRFWashington
RE_TRFHouston
RE_TRF

Dayton

So Basically, I want to create individual datasets of group , somehow I am unable to create it.

Any help is really appreciated

Thanks in advance


Accepted Solutions
Solution
‎07-13-2015 12:28 AM
Contributor
Posts: 21

Re: Create multiple datasets from single dataset

Posted in reply to forumsguy

Hi,

Please try this. Hope this will help you.

data try;

  infile datalines;

  input name $ content $;

datalines;

RE_OP Sydney

RE_OP Brussels

RE_OP Paris

RE_TRF Newyork

RE_TRF Washington

RE_TRF Houston

RE_TRF Dayton

;

run;

proc sql noprint;

  select distinct name into : x separated by ' ' from try;

quit;

%macro create;

  %do i = 1 %to %eval(%sysfunc(count(&x, %str( )))+1);

  data %scan(&x,&i.);

  set try;

  if name = "%scan(&x,&i.)";

  run;

  %end;

%mend;

%create

Br,Amit

View solution in original post


All Replies
Respected Advisor
Posts: 4,928

Re: Create multiple datasets from single dataset

Posted in reply to forumsguy

A single datastep can produce many datasets :

data RE_OP RE_TRF;

set myData;

if Name = "RE_OP"

  then output RE_OP;

  else output RE_TRF;

run;

PG

PG
Frequent Contributor
Posts: 85

Re: Create multiple datasets from single dataset

Thanks PGStats , but i dont know if my datasets will always have 2 distinct groups only, how can I generalize then ?

Super User
Posts: 19,849

Re: Create multiple datasets from single dataset

Posted in reply to forumsguy

This is a fairly common question, some of the solutions are outlined in the following link. Note the best practice - Don't do it.

The only time I've violated it, was doing a simulation and I didn't have enough disk space to store the simulated datasets.

http://www.sascommunity.org/wiki/Split_Data_into_Subsets

Solution
‎07-13-2015 12:28 AM
Contributor
Posts: 21

Re: Create multiple datasets from single dataset

Posted in reply to forumsguy

Hi,

Please try this. Hope this will help you.

data try;

  infile datalines;

  input name $ content $;

datalines;

RE_OP Sydney

RE_OP Brussels

RE_OP Paris

RE_TRF Newyork

RE_TRF Washington

RE_TRF Houston

RE_TRF Dayton

;

run;

proc sql noprint;

  select distinct name into : x separated by ' ' from try;

quit;

%macro create;

  %do i = 1 %to %eval(%sysfunc(count(&x, %str( )))+1);

  data %scan(&x,&i.);

  set try;

  if name = "%scan(&x,&i.)";

  run;

  %end;

%mend;

%create

Br,Amit

Contributor
Posts: 65

Re: Create multiple datasets from single dataset

Posted in reply to forumsguy

data Have;

input name $ contents $;

cards;

RE_OP Sydney

RE_OP Brussels

RE_OP Paris

RE_TRF Newyork

RE_TRF Washington

RE_TRF Houston

RE_TRF Dayton

;

proc sql;

create table Names as select distinct name from Have;

quit;

data _null_;

set Names;

call execute("data "||strip(name)||";set Have;where name = '"||strip(name)||"';run;");

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 503 views
  • 4 likes
  • 5 in conversation