BookmarkSubscribeRSS Feed
zqkal
Obsidian | Level 7

I have the following data set which i need to extend and fill values by looking at the previous values. 

 

data have; 
input date type $ gp1 $ grp2 $ amnt; 
datalines; 
2001 . . . 0.002
2001 MRA TL08 T630 0.0004
2001 MRA TB08 T630 0.0005
2003 . . . 0.001
2003 MRA TB08 T630 0.007
2005 . . . 0.0003
; 
run;

 

For the given date, type, gr1 and gr2 combination there is an amount specified. If the next date doesn't have the group specified on the previous date (for example, 2003 doesn't have row 2). I need to create a row for 2003 and add the amount from row 2 (0.0004 + 0.001=0.0014). 

 

Here is the desired output: 

 

date  type  grp1  grp2   cum_amt
2001 . . .   0.002
2001 MRA TL08 T630   0.0004
2001 MRA TB08 T630   0.0005
2003 . . .   0.003
2003 MRA TB08 T630   0.0075
2003 MRA TL08 T630 . 0.0014
2005         0.0003
2005 MRA TL08 T630   0.0007
2005 MRA TB08 T630   0.0078
2 REPLIES 2
WarrenKuhfeld
Rhodochrosite | Level 12

The lag function maintains a queue of values and returns the previously stored value each time you call it.  If you call it unconditionally, it will return the value from the previous observation.

ballardw
Super User

Please describe you process in terms of the variables you show. It really is not clear how "next date doesn't have the group specified " in terms of grp1 and grp2 as there are two variables but you say "group specified" and since 2003 has 2 rows in the have data I am not sure what you mean by "2003 doesn't have row 2".

 

If you describe things in terms of which values, such as the previous year group with grp1= xxx and grp2=yyy then the problem gets much clearer.

But what if there were a 2002 set of records? Would you still be looking for the 2001? Or if the previous year group is 3 years earlier such as 2001 followed by 2004. Would you want data from 2003 (which may not exist) or 2002 (which also doesn't exist) or 2001?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 636 views
  • 0 likes
  • 3 in conversation