BookmarkSubscribeRSS Feed
bhca60
Quartz | Level 8

I am trying to extract year, quarter and month from YrMth with value that looks like 202103.  It is a character format and the below isn't working:


year = year(YrMth);
Quarter = "Q" || put(QTR(YrMth),$1.);
yr = put (year,z4.);

curr_mnth = put(year(YrMth),z4.) || put(month(YrMth),z2.);
month2=put(put(month(YrMth),z2.), $mth.);  

 

3 REPLIES 3
Kurt_Bremser
Super User

The YEAR, QTR and MTH functions expect a SAS date value, which has to be numeric, not character (count of days with 1960-01-01 as day zero).

 

How did you define the $MTH format used in the last statement?

 

Please post the complete (all code and messages) log from your step.

 


@bhca60 wrote:

I am trying to extract year, quarter and month from YrMth with value that looks like 202103.  It is a character format and the below isn't working:


year = year(YrMth);
Quarter = "Q" || put(QTR(YrMth),$1.);
yr = put (year,z4.);

curr_mnth = put(year(YrMth),z4.) || put(month(YrMth),z2.);
month2=put(put(month(YrMth),z2.), $mth.);  

 


 

BrunoMueller
SAS Super FREQ

How about using the INPUT function together with appropriate INFORMAT, like so:

data test;
  yrmth = "202103";
  yrmth_sasdate = input(yrmth, yymmn6.);
  year = year(yrmth_sasdate);
  qtr = qtr(yrmth_sasdate);
  month = month(yrmth_sasdate);
  format yrmth_sasdate date9.;
run;

Now you have a SAS date variable and you can use all the functions that work with a SAS date.

ballardw
Super User

Let's parse a bit of what SAS is going to do with a character value of 202103.

 

year = year(yrmth);

The year function expects a numeric value that hopefully is a date value. So '202103' gets turned into a numeric value of 202103 by SAS attempting to correct data to the proper expected form (generating
NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column). )
Since the number value 202103 corresponds to 04MAY2513 the value of the variable year is 2513.

The QTR function then does the same conversion to numeric for YrMth, resulting in the same 04May2513 date, and reports the quarter is 2 (May being in the second quarter) and Quarter is "Q2".

The Year and Month functions repeat the same conversion so Curr_month ends up as 251305 (May being the 5th month).

I can't guess what value Month2 has because we don't have the format $mth but it would be based on May or '05' .

 

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 520 views
  • 2 likes
  • 4 in conversation