SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to make two data lines from one

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to make two data lines from one

Hi,

I need to duplicate some rows in SAS dataset and give each row a unique code: 

if name='Aaaaa' then

  duplicate row, for the first set code=1111; for the second set code =2222

Any suggestion how to do this?

Best wishes

Staffan


Accepted Solutions
Solution
‎01-10-2014 09:02 AM
Frequent Contributor
Posts: 106

Re: How to make two data lines from one

if you want that whenever condition if name='Uppsala & Lund Univ' is true then that row  is always splitted into 2 rows with code 1 and 2 then you may use this:

data have;

Input Name  $20. Code x :$ y :$ z :$;

cards;

Uppsala Univ        1 xxxx yyyy zzzz

Lund Univ           2 xxxx yyyy zzzz

Uppsala & Lund Univ 3 xxxx yyyy zzzz

Uppsala Univ        4 xxxx yyyy zzzz

Lund Univ           5 xxxx yyyy zzzz

Uppsala & Lund Univ 6 xxxx yyyy zzzz

;

data want ;

set have;

       if name='Uppsala & Lund Univ'  then do;

       code=1;

       output;

       code=2;

       end;

output;

run;

But if you want your previous row's code to be different from later rows where

if name='Uppsala & Lund Univ' is true then you may have to retain a varibale that will keep the previous count;

e.g:

data want(drop=count) ;

set have;

retain count 1;

       if name='Uppsala & Lund Univ'  then do;

       code=count;

       output;

       count+1;

       code=count;

       count+1;

       end;

output;

run;

Message was edited by: pradeep alankar

View solution in original post


All Replies
Frequent Contributor
Posts: 106

Re: How to make two data lines from one

can you example input and desired output data .

New Contributor
Posts: 3

Re: How to make two data lines from one

Input dataset:

Name                           Code

Uppsala Univ                   1

Lund Univ                        2

Uppsala & Lund Univ      3

if Name ='Uppsala & Lund Univ' the row should be slit into two rows as:

Name                             Code

Uppsala Univ                      1

Lund Univ                           2

Uppsala & Lund Univ         1

Uppsala & Lund Univ         2

The first 2 lines unchanged, while the 3rd is "split" into row 3 and 4.(with code value changed)

My original dataset has many more colums that just chould be copied as is.

Super Contributor
Posts: 578

Re: How to make two data lines from one

How should you decide which rows to split into two?

New Contributor
Posts: 3

Re: How to make two data lines from one

By

if Name ='Uppsala & Lund Univ' then split with code=1 and Code = 2

Solution
‎01-10-2014 09:02 AM
Frequent Contributor
Posts: 106

Re: How to make two data lines from one

if you want that whenever condition if name='Uppsala & Lund Univ' is true then that row  is always splitted into 2 rows with code 1 and 2 then you may use this:

data have;

Input Name  $20. Code x :$ y :$ z :$;

cards;

Uppsala Univ        1 xxxx yyyy zzzz

Lund Univ           2 xxxx yyyy zzzz

Uppsala & Lund Univ 3 xxxx yyyy zzzz

Uppsala Univ        4 xxxx yyyy zzzz

Lund Univ           5 xxxx yyyy zzzz

Uppsala & Lund Univ 6 xxxx yyyy zzzz

;

data want ;

set have;

       if name='Uppsala & Lund Univ'  then do;

       code=1;

       output;

       code=2;

       end;

output;

run;

But if you want your previous row's code to be different from later rows where

if name='Uppsala & Lund Univ' is true then you may have to retain a varibale that will keep the previous count;

e.g:

data want(drop=count) ;

set have;

retain count 1;

       if name='Uppsala & Lund Univ'  then do;

       code=count;

       output;

       count+1;

       code=count;

       count+1;

       end;

output;

run;

Message was edited by: pradeep alankar

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 450 views
  • 0 likes
  • 3 in conversation