DATA Step, Macro, Functions and more

SAS dataset split

Reply
Occasional Contributor
Posts: 19

SAS dataset split

datatset P

 

Name sex

aa       1

aa       0

aa       1

bb      0

bb      1

bb      0

 

datatset L

 

Name amount

aa       100

aa       200

aa       300

bb      400

bb      500

bb      600

 

I want to split these dataset P and L  based on name, create new datasets Z which has name as aa from both dataset.similarly for dataset K

 

output:

 

dataset z

Name sex  Name amount

aa         1        aa       100

aa         0       aa       200

aa         1      aa       300

 

 

dataset K:

 

Name sex  Name amount

bb         1     bb       400

bb        0      bb       500

bb        1      bb       600

 

I dont want to do merge. I am looking do this using set statement.

Respected Advisor
Posts: 2,810

Re: SAS dataset split

Posted in reply to suresh123

@suresh123 wrote:

 

 

I dont want to do merge. I am looking do this using set statement.


This can be done easily via a MERGE statement. I don't see how it can be done using SET.

--
Paige Miller
Super User
Posts: 6,628

Re: SAS dataset split

Posted in reply to suresh123

Your plan needs a little correction first.  It's not possible to have two variables called NAME in the same data set.  And the values for SEX for bb look like they should be 0 1 0, not 1 0 1.

 

At any rate, if both data sets contain the same number of observations for aa and bb, you could use:

 

data z k;

set p;

set L;

if name='aa' then output z;

else output k;

run;

 

Variations in the code might depend on how many NAME values exist, but this program at least works for the data you provided.

Ask a Question
Discussion stats
  • 2 replies
  • 68 views
  • 0 likes
  • 3 in conversation