I have a big data that looks like this below
ID |
event |
date |
1 |
6_W |
12/3/2010 |
1 |
8_W |
|
2 |
6_W |
4/7/2012 |
2 |
8_W |
|
3 |
6_W |
5/8/2012 |
3 |
8_W |
|
3 |
10_W |
|
I would like to add an observation above every 6_W event called “S_1” and move the date to the new observation. So it would look like this below. How can I do that?
ID |
event |
date |
1 |
S_1 |
12/3/2010 |
1 |
6_W |
|
1 |
8_W |
|
2 |
S_1 |
4/7/2012 |
2 |
6_W |
|
2 |
8_W |
|
2 |
S_1 |
5/8/2012 |
3 |
6_W |
|
3 |
8_W |
|
3 |
10_W |
|
With a slight modification to @r_behata's code above, it looks like this works:
data have;
infile datalines truncover;
input id event $ date :mmddyy10. sex $ Age Color $;
format date mmddyy10.;
datalines;
1 6_W 12/03/2020 F 12 Red
1 8_W .
2 6_W 04/07/2012 M 23 Green
2 8_W .
3 6_W 05/08/2012 M 34 Blue
3 8_W .
3 10_W .
;
run;
data want;
set have(where=(event='6_W') in=a keep=id event date)
have(in=b drop=date) ;
by id;
if a then event='S_1';
run;
Output:
Sorry, the outcome will look like this
ID |
event |
date |
1 |
S_1 |
12/3/2010 |
1 |
6_W |
|
1 |
8_W |
|
2 |
S_1 |
4/7/2012 |
2 |
6_W |
|
2 |
8_W |
|
3 |
S_1 |
5/8/2012 |
3 |
6_W |
|
3 |
8_W |
|
3 |
10_W |
|
data have;
input id event $ date :mmddyy10.;
format date mmddyy10.;
datalines;
1 6_W 12/03/2020
1 8_W .
2 6_W 04/07/2012
2 8_W .
3 6_W 05/08/2012
3 8_W .
3 10_W .
;
run;
data want;
set have(where=(event='6_W') in=a) have(in=b keep=id event) ;
by id;
if a then event='S_1';
run;
Here's one way:
data have;
input id event $ date :mmddyy10.;
format date mmddyy10.;
datalines;
1 6_W 12/03/2020
1 8_W .
2 6_W 04/07/2012
2 8_W .
3 6_W 05/08/2012
3 8_W .
3 10_W .
;
run;
data want;
set have;
if event = '6_W'
then do;
event = 'S_1';
output;
event = '6_W';
date = .;
end;
output;
run;
Since i have more than 50 variables, how do i keep the observations from those variables from not to show in s_1?
for example,
ID |
event |
date |
sex |
1 |
6_W |
12/3/2010 |
F |
1 |
8_W |
|
|
2 |
6_W |
4/7/2012 |
M |
2 |
8_W |
|
|
3 |
6_W |
5/8/2012 |
M |
3 |
8_W |
|
|
3 |
10_W |
|
|
So it would look like this below
ID |
event |
date |
|
1 |
S_1 |
12/3/2010 |
|
1 |
6_W |
|
F |
1 |
8_W |
|
|
2 |
S_1 |
4/7/2012 |
|
2 |
6_W |
|
M |
2 |
8_W |
|
|
3 |
S_1 |
5/8/2012 |
|
3 |
6_W |
|
M |
3 |
8_W |
|
|
3 |
10_W |
|
|
can someone help me on this please?
With a slight modification to @r_behata's code above, it looks like this works:
data have;
infile datalines truncover;
input id event $ date :mmddyy10. sex $ Age Color $;
format date mmddyy10.;
datalines;
1 6_W 12/03/2020 F 12 Red
1 8_W .
2 6_W 04/07/2012 M 23 Green
2 8_W .
3 6_W 05/08/2012 M 34 Blue
3 8_W .
3 10_W .
;
run;
data want;
set have(where=(event='6_W') in=a keep=id event date)
have(in=b drop=date) ;
by id;
if a then event='S_1';
run;
Output:
I have a data with more than 50 variables, i wanted to add an observation 'S_1' a above every 6_w observation in the 'Event' variable. I also wanted to move the 'date' variable observations and other two more variable observation to 'S_1" row but, keep the rest of variables observation as it is.
How can I do that? Please see the example below.
ID |
event |
date |
sex |
1 |
6_W |
12/3/2010 |
F |
1 |
8_W |
|
|
2 |
6_W |
4/7/2012 |
M |
2 |
8_W |
|
|
3 |
6_W |
5/8/2012 |
M |
3 |
8_W |
|
|
3 |
10_W |
|
|
I would like to add an observation above every 6_W event called “S_1” and move the date to the new observation. So it would look like this below
ID |
event |
date |
|
1 |
S_1 |
12/3/2010 |
|
1 |
6_W |
|
F |
1 |
8_W |
|
|
2 |
S_1 |
4/7/2012 |
|
2 |
6_W |
|
M |
2 |
8_W |
|
|
3 |
S_1 |
5/8/2012 |
|
3 |
6_W |
|
M |
3 |
8_W |
|
|
3 |
10_W |
|
|
It looks like you have this posted already.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.