BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jeremy4
Quartz | Level 8

Hi, 

 

I am creating a proc sql table and would like to join these two variables by date can someone please help me with the line of code to join them together based on the relevant month (the screenshots are only an extract of the year, so I would like to match cohort_mth, where any day of the month would be recognised as just the month e.g. 01DEC2017 or 20DEC2017 would both be recognised as DEC2017, so that it matches with the mth_end variable, which is in the mmmyyyy format)?

 

This would mean that any date and month e.g. 01FEB2017 and 15FEB2017 from cohort_mth would both be recognised as FEB2017 and this would be matched with FEB2017 from mth_end.

 

I have already matched by ID with the line (on a.customer_ID = b.ID and a.cohort_mth=b.mth_end ) - can someone please edit the red text so that they are matched together, either by recognising that it is DEC2017 or numerically 201712)? Thanks!

 

Dataset A

Cohort month.JPG

 

Dataset B

Month end.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@jeremy4 wrote:

Thanks for your reply!

 

Would that mean that in relation to my example, it would be:

on intnx('month',a.cohort_mth,0,'b') = intnx('month',b.mth_end,0,'b')

 

 


This one!

 

Not this one:

 

Would something like this also work to answer my question?

on input(put(a.cohort_mth,yymmn6.),6.) = input(put(b.mth_end,yymmn6.),6.)

 

As I stated, formatting does not really help here, it is just extra work to format the result and then unformat it. Also, unless I am missing something here, this does not convert all the dates to Feb 1 2017 which would be needed to do the matching.

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

You can't do the matching using human readable dates. SAS matches dates by SAS date values, which are integers.

 

So to match 01FEB2017 and 15FEB2017, you have to turn them both into the SAS date value integer equivalent of Feb 1, 2017.

 

You can use INTNX function to do this.

 

In a SAS data set, you could use

 

converted_date=intnx('month',date,0,'b');

The 'b' at the end selects the first day of the month in the variable DATE.

 

In PROC SQL, you could use something like this to do the matching

 

on intnx('month',a.date,0,'b') eq intnx('month',b.date,0,'b')

  

--
Paige Miller
jeremy4
Quartz | Level 8

Thanks for your reply!

 

Would that mean that in relation to my example, it would be:

on intnx('month',a.cohort_mth,0,'b') = intnx('month',b.mth_end,0,'b')

 

Would something like this also work to answer my question?

on input(put(a.cohort_mth,yymmn6.),6.) = input(put(b.mth_end,yymmn6.),6.)

Tom
Super User Tom
Super User

@jeremy4 wrote:

Thanks for your reply!

 

Would that mean that in relation to my example, it would be:

on intnx('month',a.cohort_mth,0,'b') = intnx('month',b.mth_end,0,'b')

 

Would something like this also work to answer my question?

on input(put(a.cohort_mth,yymmn6.),6.) = input(put(b.mth_end,yymmn6.),6.)


Yes.  But there is no need for the extra INPUT() function. You can just compare the strings generated by the PUT() functions.

jeremy4
Quartz | Level 8
Thanks, can I confirm that the line of code would be:
on put(a.cohort_mth,yymmn6.),6.) = put(b.mth_end,yymmn6.),6.) and a.customer_ID = b.ID
Tom
Super User Tom
Super User

You didn't completer remove the INPUT() function call.

 

on put(a.cohort_mth,yymmn6.) = put(b.mth_end,yymmn6.)
 and a.customer_ID = b.ID
PaigeMiller
Diamond | Level 26

@jeremy4 wrote:

Thanks for your reply!

 

Would that mean that in relation to my example, it would be:

on intnx('month',a.cohort_mth,0,'b') = intnx('month',b.mth_end,0,'b')

 

 


This one!

 

Not this one:

 

Would something like this also work to answer my question?

on input(put(a.cohort_mth,yymmn6.),6.) = input(put(b.mth_end,yymmn6.),6.)

 

As I stated, formatting does not really help here, it is just extra work to format the result and then unformat it. Also, unless I am missing something here, this does not convert all the dates to Feb 1 2017 which would be needed to do the matching.

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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