<?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: SCAN Function on Macro Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611166#M178078</link>
    <description>Look at the modifiers in the SCAN function and how they can change the treatment of consecutive delimiters.</description>
    <pubDate>Wed, 11 Dec 2019 21:02:03 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-12-11T21:02:03Z</dc:date>
    <item>
      <title>SCAN Function on Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611158#M178075</link>
      <description>&lt;P&gt;I have a macro variable containing the file name. The file name is in this format:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ABC_DE___January_7__2019_2019_12_20&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to extract the date January 7, 2019 from this macro variable and assign it to another macro variable using call symput.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let filename = ABC_DE___January_7__2019_2019_12_20;

data _null_;
set a;

call symput('file_year',  trim(left(%scan(&amp;amp;filename., 5, _))));
call symput('file_month', trim(left(%scan(&amp;amp;filename., 3, _))));
call symput('file_day', trim(left(%scan(&amp;amp;filename., 4, _))));

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If I call on the %put statement, I get the following results:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;file_year = 2019&lt;/P&gt;&lt;P&gt;file_day = 7&lt;/P&gt;&lt;P&gt;file_month = (blank)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the log, it says &lt;U&gt;"Variable January is uninitialized"&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't understand how the file_month could be blank considering that they are all delimited by underscores.&lt;/P&gt;&lt;P&gt;There are 3 underscores before the month; 1 underscore before the day, and 2 underscores before the year. If it's because of multiple underscores, then why was SAS able to successfully extract the file_year?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it because the month is in text form, not number form? I would need to convert the month from text to number as well. I tried adding in a input(file_month, month.) but to no avail.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Dec 2019 20:56:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611158#M178075</guid>
      <dc:creator>alyssaxm</dc:creator>
      <dc:date>2019-12-11T20:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: SCAN Function on Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611166#M178078</link>
      <description>Look at the modifiers in the SCAN function and how they can change the treatment of consecutive delimiters.</description>
      <pubDate>Wed, 11 Dec 2019 21:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611166#M178078</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-12-11T21:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: SCAN Function on Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611167#M178079</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symput('file_month', trim(left(%scan(&amp;amp;filename., 3, _))));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE&gt;&lt;CODE&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;extracts January from your text string, and then there is no data step variable named January to operate on. (You are doing this in a data step, that's why it thinks January is the name of a data step variable inside the left() function. Please note that when a macro variable or macro function is encountered in SAS code, its value is substituted and the result must be legal valid working SAS code. So the line of code for file_month resolves to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;call symput('file_month', trim(left(January)));&lt;/PRE&gt;
&lt;P&gt;which is only legal valid working SAS code if there is a variable named January.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How about this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('file_year', scan("&amp;amp;filename", 5,'_'));
call symputx('file_month', scan("&amp;amp;filename", 3,'_'));
call symputx('file_day', scan("&amp;amp;filename", 4,'_'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Dec 2019 22:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611167#M178079</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-11T22:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: SCAN Function on Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611189#M178087</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let filename = ABC_DE___January_7__2019_2019_12_20;

%let file_day     = %sysfunc(scan(&amp;amp;filename,-1,_));
%let file_month   = %sysfunc(scan(&amp;amp;filename,-2,_));
%let file_year    = %sysfunc(scan(&amp;amp;filename,-3,_));

%put &amp;amp;=file_year;
%put &amp;amp;=file_month;
%put &amp;amp;=file_day;

%symdel file_year file_month file_day;

* or if you want to use a data step ;
data _null_;
   filename    = "&amp;amp;filename";
   length file_day file_month file_year $4;
   file_day    = scan(filename,-1,'_');
   file_month  = scan(filename,-2,'_');
   file_year   = scan(filename,-3,'_');

   call symputx('file_day',file_day);
   call symputx('file_month',file_month);
   call symputx('file_year',file_year);
run;

%put &amp;amp;=file_year;
%put &amp;amp;=file_month;
%put &amp;amp;=file_day;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your mix of macro and data step statements in your data step is convoluted, I don't recommend it.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 01:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-Function-on-Macro-Variable/m-p/611189#M178087</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-12-12T01:22:41Z</dc:date>
    </item>
  </channel>
</rss>

