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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

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:

mklangley_0-1621028384917.png

 

View solution in original post

9 REPLIES 9
hjjijkkl
Pyrite | Level 9

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

 

r_behata
Barite | Level 11
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;
mklangley
Lapis Lazuli | Level 10

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;
hjjijkkl
Pyrite | Level 9

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

 

 

 

hjjijkkl
Pyrite | Level 9

can someone help me on this please?

hjjijkkl
Pyrite | Level 9
this works, but i have more than 50 variables in the data. and i only want the date to move to the event 'S_1' and keep the rest as it is. How can i do that?
mklangley
Lapis Lazuli | Level 10

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:

mklangley_0-1621028384917.png

 

hjjijkkl
Pyrite | Level 9

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

 

 

 

mklangley
Lapis Lazuli | Level 10

It looks like you have this posted already.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1749 views
  • 6 likes
  • 3 in conversation