<?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 Converting macro variable value to date format in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613239#M18375</link>
    <description>&lt;P&gt;I have a macro variable that stores the filename.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file = ABC_DE___January_7__2019_2019_12_20;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I extracted the date January 7, 2019 and stored it into macro variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file_day = %sysfunc(scan(&amp;amp;file., 4, _)); --&amp;gt; outputs 7
%let file_mth = %sysfunc(scan(&amp;amp;file., 3, _)); --&amp;gt; outputs January
%let file_yr = %sysfunc(scan(&amp;amp;file., 5, _));&amp;nbsp;&amp;nbsp;--&amp;gt; outputs&amp;nbsp;2019&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then I want to concatenate the variables above to create the file date. It outputs 2019-January-7.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file_date = %sysfunc(catx(-, &amp;amp;file_yr., &amp;amp;file_mth., &amp;amp;file_day.));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In the DATA step, I want to be able to use this file_date macro variable but in yymmdd10. format. However, I cannot convert it to this format even if I used the INPUT function. Error log shows that "January is uninitialized"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
set prevdata;

	format s_date yymmdd10.;
	s_date = input("&amp;amp;file_date.", yymmdd10.);

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you help shed some light as to why this is the case? I am a beginner when it comes to SAS and I probably am missing out on something. Thanks.&lt;/P&gt;</description>
    <pubDate>Fri, 20 Dec 2019 14:33:41 GMT</pubDate>
    <dc:creator>alyssaxm</dc:creator>
    <dc:date>2019-12-20T14:33:41Z</dc:date>
    <item>
      <title>Converting macro variable value to date format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613239#M18375</link>
      <description>&lt;P&gt;I have a macro variable that stores the filename.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file = ABC_DE___January_7__2019_2019_12_20;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I extracted the date January 7, 2019 and stored it into macro variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file_day = %sysfunc(scan(&amp;amp;file., 4, _)); --&amp;gt; outputs 7
%let file_mth = %sysfunc(scan(&amp;amp;file., 3, _)); --&amp;gt; outputs January
%let file_yr = %sysfunc(scan(&amp;amp;file., 5, _));&amp;nbsp;&amp;nbsp;--&amp;gt; outputs&amp;nbsp;2019&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then I want to concatenate the variables above to create the file date. It outputs 2019-January-7.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file_date = %sysfunc(catx(-, &amp;amp;file_yr., &amp;amp;file_mth., &amp;amp;file_day.));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In the DATA step, I want to be able to use this file_date macro variable but in yymmdd10. format. However, I cannot convert it to this format even if I used the INPUT function. Error log shows that "January is uninitialized"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
set prevdata;

	format s_date yymmdd10.;
	s_date = input("&amp;amp;file_date.", yymmdd10.);

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you help shed some light as to why this is the case? I am a beginner when it comes to SAS and I probably am missing out on something. Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 14:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613239#M18375</guid>
      <dc:creator>alyssaxm</dc:creator>
      <dc:date>2019-12-20T14:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: Converting macro variable value to date format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613240#M18376</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280192"&gt;@alyssaxm&lt;/a&gt;&amp;nbsp; Keep it simple,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file_day = %sysfunc(scan(&amp;amp;file., 4, _));
%let file_mth =%substr(%scan(&amp;amp;file,3, _),1,3); 
%let file_yr = %sysfunc(scan(&amp;amp;file., 5, _)); 


%let file_date =  &amp;amp;file_day&amp;amp;file_mth&amp;amp;file_yr;



data test;
set prevdata;
	format s_date yymmdd10.;
	s_date = "&amp;amp;file_date"d;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And actually you do not need %sysfunc to invoke Datastep/proc sql functions as you can use %scan instead of scan. So to make it cleaner, you could follow the below style&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let file_day = %scan(&amp;amp;file., 4, _));
%let file_mth =%substr(%scan(&amp;amp;file,3, _),1,3); 
%let file_yr = %scan(&amp;amp;file., 5, _); 


%let file_date =  &amp;amp;file_day&amp;amp;file_mth&amp;amp;file_yr;



data test;
set prevdata;
format s_date yymmdd10.;
s_date = "&amp;amp;file_date"d;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 14:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613240#M18376</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-20T14:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: Converting macro variable value to date format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613241#M18377</link>
      <description>&lt;P&gt;Please try the anydtdte20. format&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
set prevdata;
	s_date = input("&amp;amp;file_date.", anydtdte20.);
	format s_date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Dec 2019 14:50:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613241#M18377</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-12-20T14:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: Converting macro variable value to date format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613242#M18378</link>
      <description>&lt;P&gt;First, let's clean up the code a bit.&amp;nbsp; All the %SYSFUNCs can go.&amp;nbsp; If you switch from SCAN to %SCAN, you can use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file_day = %scan(&amp;amp;file., 4, _); --&amp;gt; outputs 7
%let file_mth = %scan(&amp;amp;file., 3, _); --&amp;gt; outputs January
%let file_yr = %scan(&amp;amp;file., 5, _);  --&amp;gt; outputs 2019&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Macro language can easily combine strings.&amp;nbsp; So in similar fashion:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file_date = &amp;amp;file_yr.-&amp;amp;file_mth.-&amp;amp;file_day.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Finally, to use this date in a DATA step, it would be most convenient to use&amp;nbsp; 7Jan2019&amp;nbsp; as the date format.&amp;nbsp; So:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let file_date2 = &amp;amp;file_day.%substr(&amp;amp;file_mth., 1, 3)&amp;amp;file_yr;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Within the DATA step no functions are needed.&amp;nbsp; You can refer to a SAS date using this syntax:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&amp;amp;file_date2."d&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 14:55:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613242#M18378</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-12-20T14:55:42Z</dc:date>
    </item>
    <item>
      <title>Re: Converting macro variable value to date format</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613252#M18380</link>
      <description>Amazing! I didn't know that I can actually remove %sysfunc and use %scan instead. Now, the code looks way simpler! I learned something today. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Thanks a lot!</description>
      <pubDate>Fri, 20 Dec 2019 15:18:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Converting-macro-variable-value-to-date-format/m-p/613252#M18380</guid>
      <dc:creator>alyssaxm</dc:creator>
      <dc:date>2019-12-20T15:18:18Z</dc:date>
    </item>
  </channel>
</rss>

