<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Converting to date format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840654#M332390</link>
    <description>&lt;P&gt;I think you are looking for an INFORMAT, not a FORMAT.&amp;nbsp; Formats convert values to text.&amp;nbsp; Informats convert text to values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to convert those strings into dates you can probably just parse the values and convert them into a date value.&amp;nbsp;This code will assume you want the first day of the month.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs      string         date

 1     01M_2017Q1    2017M01
 2     02M_2017Q1    2017M02
 3     03M_2017Q1    2017M03
 4     01M_2017Q2    2017M04
&lt;/PRE&gt;</description>
    <pubDate>Tue, 25 Oct 2022 18:36:56 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-10-25T18:36:56Z</dc:date>
    <item>
      <title>Converting to date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840634#M332382</link>
      <description>&lt;P&gt;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:&amp;nbsp;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.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Oct 2022 17:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840634#M332382</guid>
      <dc:creator>abd_khan11</dc:creator>
      <dc:date>2022-10-25T17:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: Converting to date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840641#M332383</link>
      <description>&lt;P&gt;I doubt that an out-of-the-box informat exists.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question: Would&amp;nbsp;&lt;SPAN&gt;01M_2017Q2 represent the first month of the second quarter of 2017? Meaning 01Apr2017&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Oct 2022 18:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840641#M332383</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-10-25T18:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: Converting to date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840651#M332388</link>
      <description>&lt;P&gt;I'm not sure I'd bother with a informat. Parse the string, turn the results into a SAS date value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Oct 2022 18:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840651#M332388</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-10-25T18:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: Converting to date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840654#M332390</link>
      <description>&lt;P&gt;I think you are looking for an INFORMAT, not a FORMAT.&amp;nbsp; Formats convert values to text.&amp;nbsp; Informats convert text to values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to convert those strings into dates you can probably just parse the values and convert them into a date value.&amp;nbsp;This code will assume you want the first day of the month.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs      string         date

 1     01M_2017Q1    2017M01
 2     02M_2017Q1    2017M02
 3     03M_2017Q1    2017M03
 4     01M_2017Q2    2017M04
&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Oct 2022 18:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840654#M332390</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-25T18:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Converting to date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840657#M332391</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Oct 2022 18:58:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840657#M332391</guid>
      <dc:creator>abd_khan11</dc:creator>
      <dc:date>2022-10-25T18:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Converting to date format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840659#M332393</link>
      <description>Got it. Thanks for your help! Helped clear up some concepts I was struggling with.</description>
      <pubDate>Tue, 25 Oct 2022 19:02:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-to-date-format/m-p/840659#M332393</guid>
      <dc:creator>abd_khan11</dc:creator>
      <dc:date>2022-10-25T19:02:26Z</dc:date>
    </item>
  </channel>
</rss>

