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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 13937 views
  • 4 likes
  • 5 in conversation