BookmarkSubscribeRSS Feed
Lipty
Obsidian | Level 7

Sorry for asking a stupid question.

 

I have two data sets: A: with ID and date B: with ID newsnumber date. I would like to make data set C which includes the cumulative newsnumber by ID for the past 365 days prior to the date in data A. 

 

Thank you very much for your help.

 

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

Post some sample of your data and what your desired output looks like 🙂

Lipty
Obsidian | Level 7

Data set A:

 

ID  Date

1    1998/07/21

2    2000/08/21

4    2001/04/21

6    1999/08/21

7    2000/06/21

9    2010/08/21

 

Data set B:

ID  Date Newsnumber

1

1

....

1

1

4    2000/04/22

4    2000/04/23

....

4    2000/06/22

4    2000/06/23

4    2000/06/24

...

4    2001/04/20

4    2001/04/21

6

6

6

...

6

6

6

 

For Data set C, let's take firm 4 for an example, I would like to get the cumulative sum of Newsnumber for the past 365 days before 2001/04/21.

 

Thanks~~~~ ^_^

Reeza
Super User

@Lipty Do you have SAS/ETS? If so, look into PROC EXPAND

Lipty
Obsidian | Level 7

Yes, I have it and read it last night. But didn't know how to solve for the problem. Thank you

Reeza
Super User

Post what you've tried.

mkeintz
PROC Star

I know this is a stale thread, but it was on my "recommended by SAS" box, so I took a look.  Here's a solution:

 

data want;
  merge A (rename=(date=adate))
        B ;
  by id ;
  if first.id then cumsum=0;
  if adate-365 <= date <=adate then cumsum+newsnumber;
  if last.id;
run;

 

Notes:

  1. This program assumes that dataset B is sorted by ID/date, and dataset A is sorted by ID.
  2. It also assumes that A never has more than 1 record per ID.
  3. The program assumes that the user want one record per id, even when cumsum=0.
  4. If you want to account for leap years, then the "if adate-365 <= date ..." statement should be changed to: 

    if intnx('year',adate,-1,'s') <=date <=adate then cumsum+newsnumber;

The "trick" here is to merge by id,  NOT by id date.  This results in the dataset A record merged with all the dataset B records for the same id, even though the date in A does not precede all the dates in B.  Of course you have to rename date in A, so as to not collide with date values from B.  If there is no obs in A for a given id, then adate=.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 537 views
  • 0 likes
  • 5 in conversation