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

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

1 ACCEPTED SOLUTION

Accepted Solutions
mahler_ji
Obsidian | Level 7

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

6 REPLIES 6
Astounding
PROC Star

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.

mahler_ji
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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.

mahler_ji
Obsidian | Level 7

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

mahler_ji
Obsidian | Level 7

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;

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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