<?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: Using libname to create directories when some directories not represented in data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-libname-to-create-directories-when-some-directories-not/m-p/881999#M348483</link>
    <description>&lt;P&gt;Probably you problem with DLCREATEDIR is described here:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="xis-refDictEntry"&gt;
&lt;DIV class="xis-syntax"&gt;
&lt;DIV class="xis-syntaxDescription"&gt;
&lt;DIV class="xis-otherArgGroup"&gt;
&lt;DIV id="p0bxl1gylyk4ggn1rcv0hw41wwar" class="xis-argDescriptionPair"&gt;
&lt;DIV class="xis-argumentDescription"&gt;
&lt;TABLE class="xis-summary"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="xis-restriction"&gt;Restriction&lt;/TD&gt;
&lt;TD class="xis-summaryText"&gt;If the path specified in the LIBNAME statement contains multiple components, SAS creates only the final component in the path. If any intermediate components of the path do not exist, SAS does not assign the specified path.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That means you can add ONE level to an EXISTING directory. It is quite likely that the order of your current statements is violating that. I say likely because we do not have 1) your current directory structure 2) the data set you are using to get the names 3) the code generated or 4) the actual error messages. You code would have to make sure one directory existed before making the subdirectories and there might be some system lag depending on where you are creating them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also don't see anything in the code shown that I can identify as a drive letter, disk mount point or network share as the start of a valid path.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a lot of stuff like this I would personally use use SAS to generate MKDIR or MAKEDIR statements, write them to a batch file and execute the system command statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FWIW, there is also a LIBNAME function available in data step code to create or clear libraries using data step variables. Might be easier than that macro to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 22 Jun 2023 18:47:54 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-06-22T18:47:54Z</dc:date>
    <item>
      <title>Using libname to create directories when some directories not represented in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-libname-to-create-directories-when-some-directories-not/m-p/881993#M348482</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a list of data files, each one is in a directory:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dir&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;file&lt;/P&gt;
&lt;P&gt;dira&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;file1&lt;/P&gt;
&lt;P&gt;dira/dirb&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; file2&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dira/dirc/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;file3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to move the files to a new location which is calculated based on the value of dir.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is, the new locations need to be built programatically and there are too many to do manually.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried using dlcreatedir to buidl libname statements, but this is causing errors in cases when a folder has data files but the parent folder does not.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I have:&lt;/P&gt;
&lt;DIV&gt;&lt;SPAN&gt; proc sql noprint;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; select count(directory) into :num_dirs&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; from xl_file_srt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; quit;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; %macro buildem;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; %do i= 1 %to &amp;amp;num_dirs.;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; data _null_;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; set xl_file_srt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; call symputx('lib'||strip(_n_), new_loc);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; run;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; %let lib = &amp;amp;&amp;amp;lib&amp;amp;i..;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; libname d1 "&amp;amp;lib.";&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;/* %let i = %eval(&amp;amp;i.+1);*/&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; %end;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; %mend buildem;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; %buildem;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;The only other setup other than the dlcreatedir option is that I had sorted by number of nodes/slashes .&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is much appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2023 18:16:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-libname-to-create-directories-when-some-directories-not/m-p/881993#M348482</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2023-06-22T18:16:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using libname to create directories when some directories not represented in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-libname-to-create-directories-when-some-directories-not/m-p/881999#M348483</link>
      <description>&lt;P&gt;Probably you problem with DLCREATEDIR is described here:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="xis-refDictEntry"&gt;
&lt;DIV class="xis-syntax"&gt;
&lt;DIV class="xis-syntaxDescription"&gt;
&lt;DIV class="xis-otherArgGroup"&gt;
&lt;DIV id="p0bxl1gylyk4ggn1rcv0hw41wwar" class="xis-argDescriptionPair"&gt;
&lt;DIV class="xis-argumentDescription"&gt;
&lt;TABLE class="xis-summary"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="xis-restriction"&gt;Restriction&lt;/TD&gt;
&lt;TD class="xis-summaryText"&gt;If the path specified in the LIBNAME statement contains multiple components, SAS creates only the final component in the path. If any intermediate components of the path do not exist, SAS does not assign the specified path.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That means you can add ONE level to an EXISTING directory. It is quite likely that the order of your current statements is violating that. I say likely because we do not have 1) your current directory structure 2) the data set you are using to get the names 3) the code generated or 4) the actual error messages. You code would have to make sure one directory existed before making the subdirectories and there might be some system lag depending on where you are creating them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also don't see anything in the code shown that I can identify as a drive letter, disk mount point or network share as the start of a valid path.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a lot of stuff like this I would personally use use SAS to generate MKDIR or MAKEDIR statements, write them to a batch file and execute the system command statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FWIW, there is also a LIBNAME function available in data step code to create or clear libraries using data step variables. Might be easier than that macro to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2023 18:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-libname-to-create-directories-when-some-directories-not/m-p/881999#M348483</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-22T18:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using libname to create directories when some directories not represented in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-libname-to-create-directories-when-some-directories-not/m-p/882015#M348487</link>
      <description>&lt;P&gt;Just loop over the number of levels in each directory.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let dlcreatedir=%sysfunc(getoption(dlcreatedir));
options dlcreatedir ;

data want ;
  set xl_file_srt ;
  length libname $8 path $256 ;
  libname=cats('LIB',_n_);
  do level=1 to countw(directory,'/');
    path=catx('/',path,scan(directory,level,'/'));
    rc=libname(libname,path);
  end;
run;
options &amp;amp;dlcreatedir;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't want to actually assign the librefs you could just use the same value of LIBNAME for each observation instead.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2023 19:32:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-libname-to-create-directories-when-some-directories-not/m-p/882015#M348487</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-22T19:32:57Z</dc:date>
    </item>
  </channel>
</rss>

