<?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: How to convert number or string to date with yyyymm format and create new date field based on it in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930168#M365967</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thanks a lot for the explanation of my code error and providing a solution. Really appreciate for your insights and advice.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 May 2024 18:45:31 GMT</pubDate>
    <dc:creator>LL5</dc:creator>
    <dc:date>2024-05-29T18:45:31Z</dc:date>
    <item>
      <title>How to convert number or string to date with yyyymm format and create new date field based on it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930143#M365959</link>
      <description>&lt;P&gt;Hi, I am trying to convert a six-digit number or string to a date with yyyymm format, then create two (or potentially) more new date variables, one is 12 months prior to that date, and another is 24 months prior to that date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the example. Let's say I have a pre-define date in yyyymm format (i.e 202312 for December 2023), I want to&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) convert this 202312 to a date. Since at this time I am not 100% sure if data will flow in from upstream process as a string or a numeric value, I tried to convert to both numeric and character (using input and put function, respectively)&lt;/P&gt;&lt;P&gt;2) create two more new fields. dt12 is 12 months prior to 202312, which would be 202301. dt24 is 24 months prior to 202312, which would be 202201&lt;/P&gt;&lt;P&gt;3) these new fields will be used as a filter in the where statement to extract subset of data within specific time period.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was not able to create dt12 and dt24, any advice or guidance will be greatly appreciated.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%let yyyymm_character='202312';
%let yyyymm_numeric=202312;
data test;
dt_numeric = input(&amp;amp;yyyymm_numeric,YYMMNw.);
dt_character = put(&amp;amp;yyyymm_numeric,YYMMNw.);
dt12 = intnx('month',dt_numeric,-12,'e');
dt24 = intnx('month',dt_numeric,-24,'e');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 May 2024 15:14:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930143#M365959</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2024-05-29T15:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert number or string to date with yyyymm format and create new date field based on it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930144#M365960</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yyyymm_character='202312';
%let yyyymm_numeric=202312;
data test;
dt_numeric = input(&amp;amp;yyyymm_character,YYMMN6.);
dt_character = put(&amp;amp;yyyymm_numeric,6.);
dt12 = intnx('month',dt_numeric,-12,'e');
dt24 = intnx('month',dt_numeric,-24,'e');
format dt_numeric dt12 dt24 yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Format names never end with W, the W indicates width, which must be a number, or can be left out. Your original code tried to INPUT &amp;amp;yyyymm_numeric, but INPUT works on character strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, since I dislike macro variables that have quotes around the values (I feel it is unnecessary and can cause problems in some situations), this works too&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yyyymm=202312;
data test;
dt_numeric = input("&amp;amp;yyyymm",YYMMN6.);
dt_character = put(&amp;amp;yyyymm,6.);
dt12 = intnx('month',dt_numeric,-12,'e');
dt24 = intnx('month',dt_numeric,-24,'e');
format dt_numeric dt12 dt24 yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 May 2024 15:49:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930144#M365960</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-05-29T15:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert number or string to date with yyyymm format and create new date field based on it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930146#M365961</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yyyymm_character='202312';
%let yyyymm_numeric=202312;
data test;

numeric=&amp;amp;yyyymm_numeric.;

character=&amp;amp;yyyymm_character.;

/* NUMERIC case */
dt_numeric = MDY(mod(numeric,100),01,int(numeric/100));

dt_numeric12 = intnx('month',dt_numeric,-12,'e');
dt_numeric24 = intnx('month',dt_numeric,-24,'e');

put (dt_numeric:) (/=date11.);




/* CHARACTER case */
dt_character = input(character !! '01',YYMMdd10.);
dt_character12 = intnx('month',dt_character,-12,'e');
dt_character24 = intnx('month',dt_character,-24,'e');

put (dt_character:) (/=date11.);

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 May 2024 15:45:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930146#M365961</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-05-29T15:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert number or string to date with yyyymm format and create new date field based on it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930147#M365962</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You did not provide a width, number of characters for the YYMMN informat to use so nothing was performed. Your log would show something like&lt;/P&gt;
&lt;PRE&gt;966  data test;
967  dt_numeric = input(&amp;amp;yyyymm_numeric,YYMMNw.);
                                        -------
                                        485
968  dt_character = put(&amp;amp;yyyymm_numeric,YYMMNw.);
                                        -------
                                        484
NOTE 485-185: Informat YYMMNW was not found or could not be loaded.

NOTE 484-185: Format YYMMNW was not found or could not be loaded.

969  dt12 = intnx('month',dt_numeric,-12,'e');
970  dt24 = intnx('month',dt_numeric,-24,'e');
971  run;
&lt;/PRE&gt;
&lt;P&gt;Not loaded means nothing was done because the informat could not be found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, INPUT function requires character values. If&amp;nbsp; you give SAS a numeric value then it does a default numeric to character conversion that is generally using a BEST12 format resulting in 6 leading spaces that the YYMMN informat will not use as you might want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what your log would show if you provided the proper width value for the informat:&lt;/P&gt;
&lt;PRE&gt;975  dt_numeric = input(&amp;amp;yyyymm_numeric,YYMMN6.);
976  /*dt_character = put(&amp;amp;yyyymm_numeric,YYMMNw.);*/
977  dt12 = intnx('month',dt_numeric,-12,'e');
978  dt24 = intnx('month',dt_numeric,-24,'e');
979  run;

NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
      975:20
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 977:8   1 at 978:8
&lt;/PRE&gt;
&lt;P&gt;And the result is missing values because of the conversion.&lt;/P&gt;
&lt;P&gt;So use the CHARACTER version.&lt;/P&gt;
&lt;P&gt;Note that I use a DATE9 format so you can see the result below.&lt;/P&gt;
&lt;PRE&gt;%let yyyymm_character='202312';
data test;
dt_numeric = input(&amp;amp;yyyymm_character,YYMMN6.);
dt12 = intnx('month',dt_numeric,-12,'e');
dt24 = intnx('month',dt_numeric,-24,'e');
format dt_numeric dt12 dt24 date9.;
run;&lt;/PRE&gt;
&lt;P&gt;Note: your attempt to display a simply number number, which is what 202312 is, with a date format would not show anything resembling a desired date. SAS dates are number of days since 01JAN1960. So add 20,312 days and you get a date in year 2513.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 16:02:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930147#M365962</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-29T16:02:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert number or string to date with yyyymm format and create new date field based on it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930168#M365967</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thanks a lot for the explanation of my code error and providing a solution. Really appreciate for your insights and advice.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 18:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930168#M365967</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2024-05-29T18:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert number or string to date with yyyymm format and create new date field based on it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930169#M365968</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;Thanks for illustrating the solution for both numeric and character value. Much appreciated it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 18:46:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930169#M365968</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2024-05-29T18:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert number or string to date with yyyymm format and create new date field based on it</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930171#M365970</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;Thanks a lot for analyzing the log message line by line, as well as providing a solution along with a detail explanation. Really helpful and greatly appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 18:48:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-number-or-string-to-date-with-yyyymm-format-and/m-p/930171#M365970</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2024-05-29T18:48:00Z</dc:date>
    </item>
  </channel>
</rss>

