<?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 Turning sas date into character inside a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/840985#M332506</link>
    <description>&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I feel like my issue is a simple one yet I have been stuck on this for hours. I am trying to convert a SAS date variable (datetime20. format) into a string of characters YYYYMMDD inside a macro (code lines in bold).&amp;nbsp;&lt;/P&gt;&lt;P&gt;My macro calculates the number of months between my start and end date and then executes another macro (%request) at each incremented month.&amp;nbsp; I need to transform my start date (a SAS date) to a string YYYYMMDD so that the second macro will run.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run my programme I get stuck at the first loop and I come across this note :&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE :&amp;nbsp;Macro variable CHAR_DAY resolves to inputn('01JUL2014'd,yymmddn8.). It doesn't look like it's converting my date to a string.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone help?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Juliette&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro hormono(start_dt=,end_dt=);&lt;/P&gt;&lt;P&gt;%let nbmonths=%eval(%sysfunc(intck(month,&amp;amp;start_dt.,&amp;amp;end_dt.)));&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%do i=0 %to &amp;amp;nbmonths;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%if &amp;amp;i=0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;%let char_day=inputn(&amp;amp;start_dt.,yymmddn8.);&lt;/STRONG&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %request(database, &amp;amp;char_day.);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%else %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;&amp;nbsp;%let char_day2=inputn(%sysfunc(intnx(month,&amp;amp;start_dt,&amp;amp;i,s)),yymmddn8.);&lt;/STRONG&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%request(temp, &amp;amp;char_day2.);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proc sql;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;insert into database&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;select * from temp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; quit;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%mend hormono;&lt;/P&gt;</description>
    <pubDate>Wed, 26 Oct 2022 18:00:34 GMT</pubDate>
    <dc:creator>Ju_Fr</dc:creator>
    <dc:date>2022-10-26T18:00:34Z</dc:date>
    <item>
      <title>Turning sas date into character inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/840985#M332506</link>
      <description>&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I feel like my issue is a simple one yet I have been stuck on this for hours. I am trying to convert a SAS date variable (datetime20. format) into a string of characters YYYYMMDD inside a macro (code lines in bold).&amp;nbsp;&lt;/P&gt;&lt;P&gt;My macro calculates the number of months between my start and end date and then executes another macro (%request) at each incremented month.&amp;nbsp; I need to transform my start date (a SAS date) to a string YYYYMMDD so that the second macro will run.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run my programme I get stuck at the first loop and I come across this note :&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE :&amp;nbsp;Macro variable CHAR_DAY resolves to inputn('01JUL2014'd,yymmddn8.). It doesn't look like it's converting my date to a string.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone help?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Juliette&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro hormono(start_dt=,end_dt=);&lt;/P&gt;&lt;P&gt;%let nbmonths=%eval(%sysfunc(intck(month,&amp;amp;start_dt.,&amp;amp;end_dt.)));&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%do i=0 %to &amp;amp;nbmonths;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%if &amp;amp;i=0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;%let char_day=inputn(&amp;amp;start_dt.,yymmddn8.);&lt;/STRONG&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %request(database, &amp;amp;char_day.);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%else %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;&amp;nbsp;%let char_day2=inputn(%sysfunc(intnx(month,&amp;amp;start_dt,&amp;amp;i,s)),yymmddn8.);&lt;/STRONG&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%request(temp, &amp;amp;char_day2.);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proc sql;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;insert into database&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;select * from temp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; quit;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%mend hormono;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2022 18:00:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/840985#M332506</guid>
      <dc:creator>Ju_Fr</dc:creator>
      <dc:date>2022-10-26T18:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: Turning sas date into character inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841015#M332521</link>
      <description>&lt;P&gt;Perhaps something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start_dt = %sysfunc(datetime(), datetime20.);
%put start_dt = &amp;amp;start_dt;
%let char_day = %sysfunc(datepart("&amp;amp;start_dt"dt), yymmddn8.);
%put char_day = &amp;amp;charday;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Oct 2022 19:57:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841015#M332521</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-10-26T19:57:53Z</dc:date>
    </item>
    <item>
      <title>Re: Turning sas date into character inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841018#M332524</link>
      <description>Can you show how the macro is being called?&lt;BR /&gt;And what you want to pass to REQUEST?&lt;BR /&gt;&lt;BR /&gt;You're nesting the %SYSFUNC() and INPUTN() incorrectly but not 100% sure I understand what is the input. INPUT assumes that your date comes in as YYMMDD (2014-07-01) but you said it's a datetime? If so, the datetime also requires to be converted to a date type variable.</description>
      <pubDate>Wed, 26 Oct 2022 20:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841018#M332524</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-10-26T20:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: Turning sas date into character inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841020#M332526</link>
      <description>&lt;P&gt;You can use INTCK() to count the number of months and INTNX() to generate the months.&lt;/P&gt;
&lt;P&gt;But if&amp;nbsp;you have two DATETIME values (which are NOT dates) you need to use the DTMONTH interval and not the MONTH interval.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro hormono(start_datetime,end_datetime);
%local offset  ;
%do offset = 0 %to %sysfunc(intck(dtmonth,&amp;amp;start_datetime,&amp;amp;end_datetime));
  %request(temp, %sysfunc(datepart(%sysfunc(intnx(dtmonth,&amp;amp;start_datetime,&amp;amp;offset))),yymmddn8.))
  proc append base=database data=temp;
  run;
%end;
%mend hormono;

%hormono('01JAN2020:00:00'dt,'01DEC2020:00:00'dt)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Oct 2022 20:15:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841020#M332526</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-26T20:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: Turning sas date into character inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841021#M332527</link>
      <description>&lt;P&gt;First issue is this: "convert a SAS date variable (datetime20. format) "&lt;/P&gt;
&lt;P&gt;SAS Date variables are measured in DAYS, Datetime values are measured in seconds. Since the units are different you have to pay attention to parameters used. If apply a datetime20 format to a date value you typically see results in the early 1960's, the other way around, applying a date format to a datetime you typically get out of range results for recent dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the INTNX and INTCK functions if the value is a DATETIME then you prefix intervals that involve the date portion with DT: dtday, dtmonth, dtyear and such. The RESULT is still a date time with INTNX. To get the DATE part from a DATETIME value you use the DATEPART function. INPUTING the result of INTNX is just wrong, it is a date, time or datetime value and one that if an informat actually returns a result will almost never be the date, time or datetime you think it should.&lt;/P&gt;
&lt;P&gt;Example to consider:&lt;/P&gt;
&lt;PRE&gt;data example;
   x="01Jan1960:12:00:00"dt;
   y="01MAR1960:12:00:00"dt;
   format x y datetime20.;
   z1= intck('month',x,y);
   z2= intck('dtmonth',x,y);
   put z1= z2= ;
run;&lt;/PRE&gt;
&lt;P&gt;These datetime values are pretty obviously 2 months apart. If you use MONTH for the interval the result, as in Z1 above, is 170320. Z2=2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;INPUTN is a function, just like INTCK. So you need to use %sysfunc (inputn(&amp;lt;stuff&amp;gt;) )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your partial LOG strongly implies that you passed a character value and Input with something that looks like '01JUL2014'd is just wrong. That is a date literal which SAS treats as date. Trying to input with a YYMMDDN just compounds the issue because '01JUL2014'd starts with '01, not a year, the month is the 3-letter abbreviation not the 2 digits and the ending year is just not going to work with that informat.&lt;/P&gt;
&lt;P&gt;With that said, you need to show some actual values used as your parameters Start_dt and End_dt and tell use what the expected result is for those.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2022 21:45:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841021#M332527</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-10-26T21:45:05Z</dc:date>
    </item>
    <item>
      <title>Re: Turning sas date into character inside a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841025#M332529</link>
      <description>&lt;P&gt;Here you assume that &amp;amp;start_dt is either a raw SAS date value, or a valid date literal:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%eval(%sysfunc(intck(month,&amp;amp;start_dt.,&amp;amp;end_dt.)))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But here you assume it is a string containing a date in YYYYMMDD notation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let char_day=inputn(&amp;amp;start_dt.,yymmddn8.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This function call would not work anyway, for two reasons: no %SYSFUNC wrapper, and there is no YYMMDD&lt;FONT color="#FF0000"&gt;N&lt;/FONT&gt; &lt;FONT color="#FF0000"&gt;in&lt;/FONT&gt;format.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2022 20:28:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Turning-sas-date-into-character-inside-a-macro/m-p/841025#M332529</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-26T20:28:56Z</dc:date>
    </item>
  </channel>
</rss>

