SAS Programming

DATA Step, Macro, Functions and more
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;

 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Save $200 when you sign up by March 14!

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