<?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 inside double quotes - how can I get around this? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40828#M8347</link>
    <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
Let me briefly describe the problem and what I want to do. &lt;BR /&gt;
&lt;BR /&gt;
I have 32 data files in a directory. Normally, I just import one by one, but now I want to write a macro to import them all into a library. All these 32 files have different names, of course, but they have 2 common things: TFO at the front, and OUT at the end. For example, tfo.address_type.out, or tfo_country.out. My idea for the macro is that I put the middle parts of the names of these files into a macro variable called "filenames", then I use the %SCAN macro function to extract the MIDDLE parts of those names. So, I put in this piece of codes:&lt;BR /&gt;
&lt;BR /&gt;
proc import datafile="C:\MF\Quarterly_Full_History\tfo&lt;B&gt;.%scan(&amp;amp;filenames, &amp;amp;filecount)&lt;/B&gt;.out"&lt;BR /&gt;
&lt;BR /&gt;
To my surprise, the log windows always warns that it cannot find the physical file, i.e. the %SCAN macro function doesn't work. BUT it doesn't work in a way that doesn't make sense to me. It looks like below:&lt;BR /&gt;
&lt;BR /&gt;
MPRINT(IMPORT_TFO):   proc import datafile="C:\MF\Quarterly_Full_History\&lt;B&gt;tfo..out&lt;/B&gt;" out=mf.tfo_&lt;BR /&gt;
dbms=dlm replace;&lt;BR /&gt;
MPRINT(IMPORT_TFO):   ADLM;&lt;BR /&gt;
MPRINT(IMPORT_TFO):   delimiter='|';&lt;BR /&gt;
MPRINT(IMPORT_TFO):   getnames=no;&lt;BR /&gt;
MPRINT(IMPORT_TFO):   run;&lt;BR /&gt;
&lt;BR /&gt;
ERROR: Physical file does not exist, C:\MF\Quarterly_Full_History\&lt;B&gt;tfo..out&lt;/B&gt;.&lt;BR /&gt;
&lt;BR /&gt;
And this is NOT what I thought of as why the error came, i.e. %macro function inside double quotes that treat everything inside as merely characters, because otherwise I would expect to see the full fragment "%scan(&amp;amp;filenames, &amp;amp;filecount) not resolved and treated as nothing like a string of characters. What's more, the %SCAN function inside the double quotes doesn't have the usual blue color font (syntax highlighting), so it doesn't work. &lt;BR /&gt;
&lt;BR /&gt;
Could you please help? Sorry if my question is not clear or so. I have used SAS for almost 1 year now, but only have begun to learn to use macro just recently.&lt;BR /&gt;
&lt;BR /&gt;
Thank you so much

Message was edited by: smilingmelbourne</description>
    <pubDate>Mon, 29 Nov 2010 22:54:52 GMT</pubDate>
    <dc:creator>smilingmelbourne</dc:creator>
    <dc:date>2010-11-29T22:54:52Z</dc:date>
    <item>
      <title>%SCAN inside double quotes - how can I get around this?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40825#M8344</link>
      <description>Hi everyone,&lt;BR /&gt;
&lt;BR /&gt;
I am trying to import a series of files with different names. The macro didn't work and I sort of know why, but I don't know how to get around this still using a revised macro (and not avoiding the macro). The error originates from using the macro quoting function %SCAN inside double quotes, in which everything is treated as a character/string of characters, and as a result, this function is useless. How can I fix this error? Thank you very much. &lt;BR /&gt;
&lt;BR /&gt;
Here is the code&lt;BR /&gt;
&lt;BR /&gt;
%macro import_tfo;&lt;BR /&gt;
%local filenames filecount;&lt;BR /&gt;
%let &lt;B&gt;filesnames= address_type&lt;BR /&gt;
	         country&lt;/B&gt;                          /*a lot of filenames in here*/&lt;BR /&gt;
&lt;BR /&gt;
								;&lt;BR /&gt;
&lt;B&gt;%do filecount=1 %to 32 %by 1;&lt;/B&gt;        &lt;BR /&gt;
proc 	import datafile="C:\TMP\&lt;B&gt;tfo&lt;/B&gt;&lt;B&gt;.%scan[/b&lt;B&gt;](&amp;amp;filenames, &amp;amp;filecount).out&lt;/B&gt;"&lt;BR /&gt;
	out=mf.tfo_%scan(&amp;amp;filenames, &amp;amp;filecount)&lt;BR /&gt;
	dbms=dlm replace;&lt;BR /&gt;
	delimiter='|';&lt;BR /&gt;
	getnames=no;&lt;BR /&gt;
	run;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend import_tfo;&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: smilingmelbourne&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: smilingmelbourne

Message was edited by: smilingmelbourne&lt;/B&gt;</description>
      <pubDate>Mon, 29 Nov 2010 11:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40825#M8344</guid>
      <dc:creator>smilingmelbourne</dc:creator>
      <dc:date>2010-11-29T11:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: %SCAN inside double quotes - how can I get around this?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40826#M8345</link>
      <description>although I'm tempted to say "don't use the PROC", let me just address the macro&lt;BR /&gt;
 &lt;BR /&gt;
add a local mVar holding the value you need.....&lt;BR /&gt;
(and remember that the OUT= parameter needs a valid name so no special characters, other than underscore, in the  &amp;amp;filenames  )&lt;BR /&gt;
;&lt;BR /&gt;
&amp;gt; %do filecount=1 %to 32 %by 1;&lt;BR /&gt;
 %local ref ; &lt;BR /&gt;
 %let ref = %unQuote(%scan(&amp;amp;filenames, &amp;amp;filecount)) ;&lt;BR /&gt;
&amp;gt; proc 	import&lt;BR /&gt;
 datafile="C:\TMP\tfo.&amp;amp;ref..out"&lt;BR /&gt;
 	out=mf.tfo_&amp;amp;ref&lt;BR /&gt;
&amp;gt; 	dbms=dlm replace;&lt;BR /&gt;
&amp;gt; 	delimiter='|';&lt;BR /&gt;
&amp;gt; 	getnames=no;&lt;BR /&gt;
&amp;gt; 	run;&lt;BR /&gt;
&amp;gt; %end;&lt;BR /&gt;
&amp;gt; %mend import_tfo;</description>
      <pubDate>Mon, 29 Nov 2010 12:30:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40826#M8345</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2010-11-29T12:30:20Z</dc:date>
    </item>
    <item>
      <title>Re: %SCAN inside double quotes - how can I get around this?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40827#M8346</link>
      <description>I am interested in how the %SCAN function is failing.  While there can be timing issues when working with paths, there is not a problem with macro functions inside of double quotes.</description>
      <pubDate>Mon, 29 Nov 2010 20:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40827#M8346</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-11-29T20:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: %SCAN inside double quotes - how can I get around this?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40828#M8347</link>
      <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
Let me briefly describe the problem and what I want to do. &lt;BR /&gt;
&lt;BR /&gt;
I have 32 data files in a directory. Normally, I just import one by one, but now I want to write a macro to import them all into a library. All these 32 files have different names, of course, but they have 2 common things: TFO at the front, and OUT at the end. For example, tfo.address_type.out, or tfo_country.out. My idea for the macro is that I put the middle parts of the names of these files into a macro variable called "filenames", then I use the %SCAN macro function to extract the MIDDLE parts of those names. So, I put in this piece of codes:&lt;BR /&gt;
&lt;BR /&gt;
proc import datafile="C:\MF\Quarterly_Full_History\tfo&lt;B&gt;.%scan(&amp;amp;filenames, &amp;amp;filecount)&lt;/B&gt;.out"&lt;BR /&gt;
&lt;BR /&gt;
To my surprise, the log windows always warns that it cannot find the physical file, i.e. the %SCAN macro function doesn't work. BUT it doesn't work in a way that doesn't make sense to me. It looks like below:&lt;BR /&gt;
&lt;BR /&gt;
MPRINT(IMPORT_TFO):   proc import datafile="C:\MF\Quarterly_Full_History\&lt;B&gt;tfo..out&lt;/B&gt;" out=mf.tfo_&lt;BR /&gt;
dbms=dlm replace;&lt;BR /&gt;
MPRINT(IMPORT_TFO):   ADLM;&lt;BR /&gt;
MPRINT(IMPORT_TFO):   delimiter='|';&lt;BR /&gt;
MPRINT(IMPORT_TFO):   getnames=no;&lt;BR /&gt;
MPRINT(IMPORT_TFO):   run;&lt;BR /&gt;
&lt;BR /&gt;
ERROR: Physical file does not exist, C:\MF\Quarterly_Full_History\&lt;B&gt;tfo..out&lt;/B&gt;.&lt;BR /&gt;
&lt;BR /&gt;
And this is NOT what I thought of as why the error came, i.e. %macro function inside double quotes that treat everything inside as merely characters, because otherwise I would expect to see the full fragment "%scan(&amp;amp;filenames, &amp;amp;filecount) not resolved and treated as nothing like a string of characters. What's more, the %SCAN function inside the double quotes doesn't have the usual blue color font (syntax highlighting), so it doesn't work. &lt;BR /&gt;
&lt;BR /&gt;
Could you please help? Sorry if my question is not clear or so. I have used SAS for almost 1 year now, but only have begun to learn to use macro just recently.&lt;BR /&gt;
&lt;BR /&gt;
Thank you so much

Message was edited by: smilingmelbourne</description>
      <pubDate>Mon, 29 Nov 2010 22:54:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40828#M8347</guid>
      <dc:creator>smilingmelbourne</dc:creator>
      <dc:date>2010-11-29T22:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: %SCAN inside double quotes - how can I get around this?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40829#M8348</link>
      <description>Thank you all for your help. After hours of trying to figure out where the error comes from, I finally have found it. It's a typo!!! Filename sometimes get spelled as fileSname... The problem is that even the log window doesn't really help much for a beginner/inexperienced user (unlike Matlab, Stata) &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;</description>
      <pubDate>Mon, 29 Nov 2010 23:29:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40829#M8348</guid>
      <dc:creator>smilingmelbourne</dc:creator>
      <dc:date>2010-11-29T23:29:28Z</dc:date>
    </item>
    <item>
      <title>Re: %SCAN inside double quotes - how can I get around this?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40830#M8349</link>
      <description>You probably also determined that there is an extra . (dot/period) between the TFO and the %SCAN.</description>
      <pubDate>Tue, 30 Nov 2010 06:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40830#M8349</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-11-30T06:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: %SCAN inside double quotes - how can I get around this?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40831#M8350</link>
      <description>&amp;gt; Hi everyone,&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; I am trying to import a series of files with&lt;BR /&gt;
&amp;gt; different names. The macro didn't work and I sort of&lt;BR /&gt;
&amp;gt; know why, but I don't know how to get around this&lt;BR /&gt;
&amp;gt; still using a revised macro (and not avoiding the&lt;BR /&gt;
&amp;gt; macro). The error originates from using the macro&lt;BR /&gt;
&amp;gt; quoting function %SCAN inside double quotes, in which&lt;BR /&gt;
&amp;gt; everything is treated as a character/string of&lt;BR /&gt;
&amp;gt; characters, and as a result, this function is&lt;BR /&gt;
&amp;gt; useless. How can I fix this error? Thank you very&lt;BR /&gt;
&amp;gt; much. &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Here is the code&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; %macro import_tfo;&lt;BR /&gt;
&amp;gt; %local filenames filecount;&lt;BR /&gt;
&amp;gt; %let &lt;B&gt;filesnames= address_type&lt;BR /&gt;
&amp;gt; country&lt;/B&gt;                          /*a&lt;BR /&gt;
&amp;gt;       /*a lot of filenames in here*/&lt;BR /&gt;
&amp;gt; 				;&lt;BR /&gt;
&amp;gt; &lt;B&gt;%do filecount=1 %to 32 %by 1;&lt;/B&gt;        &lt;BR /&gt;
&amp;gt; proc 	import&lt;BR /&gt;
&amp;gt; datafile="C:\TMP\&lt;B&gt;tfo&lt;/B&gt;&lt;B&gt;.%scan[/b&lt;B&gt;](&amp;amp;filenames&lt;BR /&gt;
&amp;gt; , &amp;amp;filecount).out&lt;/B&gt;"&lt;BR /&gt;
&amp;gt; 	out=mf.tfo_%scan(&amp;amp;filenames, &amp;amp;filecount)&lt;BR /&gt;
&amp;gt; 	dbms=dlm replace;&lt;BR /&gt;
&amp;gt; 	delimiter='|';&lt;BR /&gt;
&amp;gt; 	getnames=no;&lt;BR /&gt;
&amp;gt; 	run;&lt;BR /&gt;
&amp;gt; %end;&lt;BR /&gt;
&amp;gt; %mend import_tfo;&lt;/B&gt;&lt;BR /&gt;
...&lt;BR /&gt;
A better way to write a macro for something like this is to write two macros, instead of one. One for doing a task (here importing a file), and another for looping over a list of items. In this way, you first write the first one and test it out, and then when it works well, you concentrate doing the second step (or not). Hope this helps a bit.&lt;BR /&gt;
[pre]&lt;BR /&gt;
   %macro doOne(datafile=);&lt;BR /&gt;
     %if %superq(datafile)= %then %return;&lt;BR /&gt;
     %*-- do something with datafile --*;&lt;BR /&gt;
     %put datafile=&amp;amp;datafile;&lt;BR /&gt;
   %mend  doOne;&lt;BR /&gt;
&lt;BR /&gt;
   %macro doAll(list=);&lt;BR /&gt;
     %local el i;&lt;BR /&gt;
     %let i = 1;&lt;BR /&gt;
     %let el = %scan(&amp;amp;list, &amp;amp;i);&lt;BR /&gt;
     %do %while(&amp;amp;el^=);&lt;BR /&gt;
       %doOne(datafile=&amp;amp;el)&lt;BR /&gt;
       %let i = %eval(&amp;amp;i + 1);&lt;BR /&gt;
       %let el = %scan(&amp;amp;list, &amp;amp;i);&lt;BR /&gt;
     %end;&lt;BR /&gt;
   %mend  doAll;&lt;BR /&gt;
&lt;BR /&gt;
   %*-- you can test one or two --*;&lt;BR /&gt;
   %doOne(datafile=a)&lt;BR /&gt;
   %doOne(datafile=b)&lt;BR /&gt;
   %*-- on log&lt;BR /&gt;
   datafile=a&lt;BR /&gt;
   datafile=b&lt;BR /&gt;
   --*;&lt;BR /&gt;
&lt;BR /&gt;
   %*-- if it works well and do a lot --*;&lt;BR /&gt;
   %doAll(list=c d e f g)&lt;BR /&gt;
   %*-- on log&lt;BR /&gt;
   datafile=c&lt;BR /&gt;
   datafile=d&lt;BR /&gt;
   datafile=e&lt;BR /&gt;
   datafile=f&lt;BR /&gt;
   datafile=g&lt;BR /&gt;
   --*;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 07 Dec 2010 20:06:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/40831#M8350</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-12-07T20:06:19Z</dc:date>
    </item>
    <item>
      <title>Re: %SCAN inside double quotes - how can I get around this?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/807876#M318559</link>
      <description>&lt;P&gt;Say y = %scan(&amp;amp;list, &amp;amp;k., ",")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now if you need character output in double quotes, you just need to make the following change:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;y = "%scan(&amp;amp;list, &amp;amp;k., ",")"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also use %scan in a loop only&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 16:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SCAN-inside-double-quotes-how-can-I-get-around-this/m-p/807876#M318559</guid>
      <dc:creator>vkabdwal</dc:creator>
      <dc:date>2022-04-14T16:55:46Z</dc:date>
    </item>
  </channel>
</rss>

