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

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

1 ACCEPTED SOLUTION

Accepted Solutions
pradeepalankar
Obsidian | Level 7

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

5 REPLIES 5
pradeepalankar
Obsidian | Level 7

can you example input and desired output data .

StaffanKarlsson
Calcite | Level 5

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.

DBailey
Lapis Lazuli | Level 10

How should you decide which rows to split into two?

StaffanKarlsson
Calcite | Level 5

By

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

pradeepalankar
Obsidian | Level 7

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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