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

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

1 ACCEPTED SOLUTION

Accepted Solutions
AmitRathore
Obsidian | Level 7

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

5 REPLIES 5
PGStats
Opal | Level 21

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
forumsguy
Fluorite | Level 6

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

Reeza
Super User

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

AmitRathore
Obsidian | Level 7

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

MadhuKorni
Quartz | Level 8

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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