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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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