BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
abd_khan11
Calcite | Level 5

Hello! I'm new to SAS and was performing some data cleansing. I'm aware of the existence of a variety of date formats and was curious as to whether there was a relevant one for the one I'm trying to convert. The dates look like this: 01M_2017Q1 (01=first month of quarter, Q1=first quarter). I'd like to convert this to a date that provides the month and year of the given date. Any and all help is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I think you are looking for an INFORMAT, not a FORMAT.  Formats convert values to text.  Informats convert text to values.

 

If you want to convert those strings into dates you can probably just parse the values and convert them into a date value. This code will assume you want the first day of the month. 

 

You can then attach any of the many date formats to the variable to display it in a way that humans will recognize. YYMM7 is one that displays only the year and month part.

data have ;
  input string $10. ;
cards;
01M_2017Q1
02M_2017Q1
03M_2017Q1
01M_2017Q2
;

data want;
  set have;
  date = intnx('month',input(scan(string,2,'_'),yyq6.),input(string,2.)-1);
  format date yymm7.;
run;

Result:

Obs      string         date

 1     01M_2017Q1    2017M01
 2     02M_2017Q1    2017M02
 3     03M_2017Q1    2017M03
 4     01M_2017Q2    2017M04

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

I doubt that an out-of-the-box informat exists.

 

Question: Would 01M_2017Q2 represent the first month of the second quarter of 2017? Meaning 01Apr2017

PaigeMiller
Diamond | Level 26

I'm not sure I'd bother with a informat. Parse the string, turn the results into a SAS date value.

 

data want;
    date_string='01M_2017Q1';
    month=substr(date_string,1,2);
    year=substr(date_string,5,4);
    quarter=substr(date_string,10,1);
    sas_date=mdy((input(quarter,1.)-1)*3+input(month,2.),1,input(year,4.));
    format sas_date date9.; /* Comment out this line to leave the date value unformatted */
run;

 

 

To perhaps clear up the terminology, you need an informat (not a format) to read a text string into a date. But such an informat does not exist. You (and the code above) are not converting the character string to a "date format", it is converting a character string to a valid numeric SAS date value. Once you have a valid numeric SAS date value, you can then apply any valid SAS date format to it, or leave it unformatted.

--
Paige Miller
abd_khan11
Calcite | Level 5

Hi! I didn't know those discrepancies existed, thanks so much for the info and your help. Will be really useful moving forward with my work. 

Tom
Super User Tom
Super User

I think you are looking for an INFORMAT, not a FORMAT.  Formats convert values to text.  Informats convert text to values.

 

If you want to convert those strings into dates you can probably just parse the values and convert them into a date value. This code will assume you want the first day of the month. 

 

You can then attach any of the many date formats to the variable to display it in a way that humans will recognize. YYMM7 is one that displays only the year and month part.

data have ;
  input string $10. ;
cards;
01M_2017Q1
02M_2017Q1
03M_2017Q1
01M_2017Q2
;

data want;
  set have;
  date = intnx('month',input(scan(string,2,'_'),yyq6.),input(string,2.)-1);
  format date yymm7.;
run;

Result:

Obs      string         date

 1     01M_2017Q1    2017M01
 2     02M_2017Q1    2017M02
 3     03M_2017Q1    2017M03
 4     01M_2017Q2    2017M04
abd_khan11
Calcite | Level 5
Got it. Thanks for your help! Helped clear up some concepts I was struggling with.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2578 views
  • 0 likes
  • 4 in conversation