Help using Base SAS procedures

Convert Character Date to SAS Date

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 101
Accepted Solution

Convert Character Date to SAS Date

Hello,

I have dates that are important for merging that come in the following character format:

1998Q1

1998Q2

1991Q1


I would like these to be turned into SAS dates, so that I can change the format, and use them to merge with other SAS dates.  I would like to merge with another dataset that has SAS dates in the yyq. format, after using intnx("QTR", date, 0) to turn the dates into quarter measures.

I am using the following code, but that is not working:

data want;

  set have;

  newdate = input(put(date,6.),YYQ.);

  format newdate yyq.;

run;

I know that this is probably pretty simple, but just let me know.

Thanks,

John


Accepted Solutions
Solution
‎02-20-2015 02:29 PM
Frequent Contributor
Posts: 101

Re: Convert Character Date to SAS Date

Ok, I was able to figure it out, I just needed to use the correct INFORMAT in the input statement.  The following code works, and I created the merging date variable, called fp, using the INTNX statement:

data want;

  set have;

  newdate = input(date, YYQ6.);

  fp = intnx("QTR", newdate, 0);

  format fp yyq.;

run;

View solution in original post


All Replies
Super User
Posts: 5,081

Re: Convert Character Date to SAS Date

Let's start with basics ... what are you trying to do?  Create a new character variable?  Create a numeric date equivalent to a particular day on SAS's date scale?  The answer might depend on the contents of the "other" data set(s) that you would like to merge with.

Frequent Contributor
Posts: 101

Re: Convert Character Date to SAS Date

I just need it in SAS Date YYQ. format, so that I can merge it with the other data set, in which I have the dates in yyq. SAS Date format.

Super User
Super User
Posts: 6,499

Re: Convert Character Date to SAS Date

You can use the INPUT() function to convert your character variable to a date.

data _null_;

  input S $6. ;

  date = input(S,YYQ6.);

  put s= date= +1 date YYQ6. +1 date DATE9. ;

cards;

1998Q1

1998Q2

;;;;

S=1998Q1 date=13880  1998Q1 01JAN1998

S=1998Q2 date=13970  1998Q2 01APR1998

But if you have a DATE variable in the other table that is formatted to display as YYQ6 you need to also check if the values are consistent with the date that SAS will assign if you convert your current string to a date.  SAS will set the value to the first day of the QTR.  But it is possible that the other dataset has other days during the quarter.  In that case a simple MERGE will not work as the internal values will not match.

Frequent Contributor
Posts: 101

Re: Convert Character Date to SAS Date

Hey Tom,

I just updated the first post with a little more detail.   The dates that are in the dataset that I would like to merge with have been converted to quarters using the INTNX function, so I will not be hindered by the first day of the month, etc.

In actuality, the format that the character date is in (1998Q1) is perfect, but I need it to be converted to a SAS date and not a character variable.

Is there an easy data step way to do this, so that I don't have to input every single date individually with a cards statement?  The dataset that I have has about 10 million observations.

Thanks,

John

Solution
‎02-20-2015 02:29 PM
Frequent Contributor
Posts: 101

Re: Convert Character Date to SAS Date

Ok, I was able to figure it out, I just needed to use the correct INFORMAT in the input statement.  The following code works, and I created the merging date variable, called fp, using the INTNX statement:

data want;

  set have;

  newdate = input(date, YYQ6.);

  fp = intnx("QTR", newdate, 0);

  format fp yyq.;

run;

Super User
Super User
Posts: 6,499

Re: Convert Character Date to SAS Date

The FP variable will have the exact same values of the NEWDATE variable.  When you use the YYQ6. informat it will set the date to the first day in the quarter, which is what your INTNX() function call is doing.

You still might want to keep them since you could assign different formats to them.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 2187 views
  • 1 like
  • 3 in conversation