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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 5 replies
  • 1362 views
  • 0 likes
  • 4 in conversation