<?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: Special case in retrieving names of all file in one folder in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713828#M220295</link>
    <description>&lt;P&gt;Replace the substr() function into scan() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;short_fn = cats(scan(fname,1,'_'),'_');&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 25 Jan 2021 06:38:01 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2021-01-25T06:38:01Z</dc:date>
    <item>
      <title>Special case in retrieving names of all file in one folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713818#M220289</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;I have a problem when trying to run the macro ImportAndTranspose for all files in one folder (named &lt;STRONG&gt;New Folder&lt;/STRONG&gt;). The filename is the name of countries with an under space, for example: &lt;STRONG&gt;Argentina_, Austria_, Australia_, Belgium_, US_, UK_&lt;/STRONG&gt;,..... and I have 64 files like that.&lt;/P&gt;
&lt;P&gt;The code is as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename mydir 'C:\Users\pnguyen\Desktop\New folder';
data _null_;
did = dopen('mydir');
do i = 1 to dnum(did);
  fname = dread(did,i);
  short_fn= cats(substr(fname, 1,3),'_');
  cmd=cats('%ImportAndTranspose(File=C:\Users\pnguyen\Desktop\New folder\',
      strip(fname),',outf=',short_fn,'sheet,startsheet=1,endsheet=45);');
  call execute(cmd);
end;
keep fname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now I just recognize that if I do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;short_fn= cats(substr(fname, 1,3),'_');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;So, the &lt;STRONG&gt;short_fn&lt;/STRONG&gt; for &lt;STRONG&gt;Australia &lt;/STRONG&gt;and&lt;STRONG&gt; Austria&lt;/STRONG&gt; or &lt;STRONG&gt;Slovakia&lt;/STRONG&gt; and &lt;STRONG&gt;Slovenia&lt;/STRONG&gt; are the same&lt;/FONT&gt;, but if I did&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;short_fn= cats(substr(fname, 1,4),'_');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;UK_&lt;/STRONG&gt; and &lt;STRONG&gt;US_&lt;/STRONG&gt; do not have enough character in their names, and it make make the results' name in general not aesthetic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please give me any suggestion to adjust the code to solve the problem?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks and warm regards.&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 25 Jan 2021 05:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713818#M220289</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-25T05:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: Special case in retrieving names of all file in one folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713819#M220290</link>
      <description>&lt;P&gt;Try something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input fname $10.;
datalines;
Australia 
Austria   
Slovakia  
UK        
US        
;

data want;
   set have;
   short_fn = cats(substr(fname, 1, min(3, length(fname))), '_');
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2021 05:42:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713819#M220290</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-25T05:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: Special case in retrieving names of all file in one folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713821#M220291</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result is as below:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="My97_0-1611553865394.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53856i018AFE3603E6A061/image-size/medium?v=v2&amp;amp;px=400" role="button" title="My97_0-1611553865394.png" alt="My97_0-1611553865394.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I mean, one reasonable solution is to get Austra_ for Australia, Austri_ for Austria, UK_ for the UK, US_ for the US, and the other files are normal for example: ARG_ for Argentina, CHI_ for China, so on and so forth.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;DIV id="eJOY__extension_root" class="eJOY__extension_root_class" style="all: unset;"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 25 Jan 2021 05:55:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713821#M220291</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-25T05:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: Special case in retrieving names of all file in one folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713825#M220294</link>
      <description>&lt;P&gt;Ok. Obviously, you can do this with simple if-then logic. I will spare you the details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For maintainability and length control, this is a good case using user-defined Function Logic in Proc Format. Here, I simply make 'special' cases for some countries, while the rest is sent to the function f, which does the work. Then all that is needed is a Put Function in a data step.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Feel free to ask &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input fname $10.;
datalines;
Australia 
Austria   
Slovakia  
UK        
US        
;

proc fcmp outlib=work.f.f;
   function f(s $) $;
      length r $ 4;
      r = upcase(cats(substr(s, 1, 3), '_'));
      return (r);
   endsub;
run;

options cmplib = work.f;
proc format;
   value $ country (default = 12)
      'Australia' = 'Austra_'
      'Austria'   = 'Austri_'
      'UK'        = 'UK_'
      'US'        = 'US_'
      other       = [f()]
   ;
run;

data want;
   set have;
   short_fn = put(fname, $country.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2021 06:22:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713825#M220294</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-25T06:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Special case in retrieving names of all file in one folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713828#M220295</link>
      <description>&lt;P&gt;Replace the substr() function into scan() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;short_fn = cats(scan(fname,1,'_'),'_');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2021 06:38:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713828#M220295</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-25T06:38:01Z</dc:date>
    </item>
    <item>
      <title>Re: Special case in retrieving names of all file in one folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713853#M220305</link>
      <description>&lt;P&gt;Do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length shortfn $4;
shortfn = scan(fname,1,"_");&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2021 08:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Special-case-in-retrieving-names-of-all-file-in-one-folder/m-p/713853#M220305</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-25T08:47:34Z</dc:date>
    </item>
  </channel>
</rss>

