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
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
can you example input and desired output data .
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.
How should you decide which rows to split into two?
By
if Name ='Uppsala & Lund Univ' then split with code=1 and Code = 2
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.