BookmarkSubscribeRSS Feed
DangIT
Fluorite | Level 6

Hi,

I have the following tables:

 

UserTxnTbl

UserID FileDate ProdGroup 

1 01Jan2015 A

1 01Feb2015 A

1 01Mar2015 A

 

UserCohortTbl - a user can exist in multiple Cohorts dependent on FileDate

UserID FileDate Cohort 

1 01Jan2015 01Feb2014

1 01Mar2015  01Jun2014

 

I am trying to create a dataset that adds Cohort to UserTxnTbl where it pulls the Cohort value that is Less Than the FileDate in the UserTxnTbl

 

Want

UserID FileDate ProdGroup Cohort

1 01Jan2015 A 01Feb2014

1 01Feb2015 A 01Feb2014

1 01Mar2015 A 01Jun2014

 

 

Thank you for your help!

 

5 REPLIES 5
LinusH
Tourmaline | Level 20
The logic is not clear. Both dates in Cohort are earlier than all file dates in Txn.
Describe the business requirement and a more thorough example for a better understanding.
Data never sleeps
ballardw
Super User

Also, are the dates SAS date valued numerics displayed with a Date9. format or character values?

Ksharp
Super User

Yes. It is not clear . what if the tables like the following, what you gonna do ?

 

UserTxnTbl

UserID FileDate ProdGroup 

1 01Jan2015 A

1 01Feb2015 A

1 01Mar2015 A

 

UserCohortTbl - a user can exist in multiple Cohorts dependent on FileDate

UserID FileDate Cohort 

1 01Jan2015 01Feb2014

1 01Feb2014 01Mar2015

1 01Mar2015  01Jun2014

DangIT
Fluorite | Level 6

Sorry let me start this again:

 

 

UserTxnTbl: A table that captures all transactions of a product group at the beginning of the month for all users starting 01Jan2015 and going forward. If there is no transactions the FileDate will not exist for that month.

UserID FileDate ProdGroup 

1 01Jan2015 A

1 01Feb2015 A

1 01Mar2015 A

 

UserCohortTbl - A table that has analyzed the history of users and classified them into a Cohort at a point in time. From 01Feb2014 to 01Feb2015 User 1 belonged to Cohort 01Feb2014. In March 2015 they may have changed some sort of behaviour that redefined their cohort going forward.

UserID Cohort 

1 01Feb2014 

1 01Mar2015

 

Ultimately I am looking to add Cohort to the UserTxnTbl to identify which Cohort the user belonged to at the FileDate period.

 

Want

UserID FileDate ProdGroup Cohort

1 01Jan2015 A 01Feb2014

1 01Feb2015 A 01Feb2014

1 01Mar2015 A 01Mar2015

 

Apologies again for the vaugeness in the original post. After typing this out again, i'm thinking Proc Format might work??

Thanks again for your help

Ksharp
Super User

OK. Looks more clear now. Assuming I understand what do you mean.

 

data UserTxnTbl;
input UserID FileDate : date9. ProdGroup $;
format FileDate  date9.;
cards; 
1 01Jan2015 A
1 01Feb2015 A
1 01Mar2015 A
2 01Jan2015 A
2 01Feb2015 A
2 01Mar2015 A
run;
 
data UserCohortTbl ;
input UserID Cohort : date9.;
format Cohort date9.;
cards; 
1 01Feb2014 
1 01Mar2015
2 01Feb2014 
2 01Mar2015
;
run;
data want;
 set UserCohortTbl(rename=(Cohort=FileDate) in=ina) UserTxnTbl(in=inb);
 by UserID FileDate;
 retain Cohort;
 if first.UserID then call missing(Cohort);
 if ina then Cohort=FileDate;
 if inb;
 format Cohort date9.;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 5 replies
  • 1195 views
  • 0 likes
  • 4 in conversation