BookmarkSubscribeRSS Feed
mattmm
Fluorite | Level 6

Have: Data ordered by unique Code, by unique ID, with static Date1 and static Item1.

Code1

Date1

Item1

Id

1

2/23/21

5

1

1

2/23/21

9

2

1

2/23/21

6

3

2

2/23/21

7

1

2

2/23/21

3

2

3

2/23/21

4

1

 

Want:  When ID eq 1 then Want1= Item1 + Date1, else add Item1 to Want1 while retaining the value of the previous Want1 until the end of the ID of Code1.

Code1

Date1

Item1

Id

Want1

1

2/23/21

5

1

2/28/21

1

2/23/21

9

2

3/9/21

1

2/23/21

6

3

3/15/21

2

2/23/21

7

1

3/2/21

2

2/23/21

3

2

3/5/21

3

2/23/21

4

1

2/27/21

 

Code1

Date1

Item1

Id

Want1

1

2/23/21

5

1

2/28/21

1

2/23/21

9

2

3/9/21

1

2/23/21

6

3

3/15/21

2

2/23/21

7

1

3/2/21

2

2/23/21

3

2

3/5/21

3

2/23/21

4

1

2/27/21

 

I have experimented with the code below but I'm unsure how to keep the created value of want1 when id =1, and then add item1 when id ne 1 by code1. Any thoughts or help is greatly appreciated.

data want; set have;
by code1 date1 id;
if first.id then want1 = item1+date1;
           else ?
run;
1 REPLY 1
mattmm
Fluorite | Level 6

I figured it out by utilizing retain. 

data want; set have;
by code1 date1 id;
retain want1;
if id=1 then want1=date1+item1;
else want1= want1+ item1;
run;

 

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
  • 1 reply
  • 379 views
  • 0 likes
  • 1 in conversation