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.
Post some sample of your data and what your desired output looks like 🙂
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~~~~ ^_^
@Lipty Do you have SAS/ETS? If so, look into PROC EXPAND
Yes, I have it and read it last night. But didn't know how to solve for the problem. Thank you
Post what you've tried.
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:
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=.
Use the macro provided in https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... to convert your datasets to data steps, or write your own data steps that create example data.
Post the resulting code according to https://communities.sas.com/t5/help/faqpage/faq-category-id/posting?nobounce (How can I add SAS syntax to a post?)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.