<?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: Keep only the first row on all the datasets in a directory and all of its subdirectories in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-the-first-row-on-all-the-datasets-in-a-directory-and/m-p/866846#M342344</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename dirtree url 'https://raw.githubusercontent.com/sasutils/macros/master/dirtree.sas';

%include dirtree;

%dirtree(directory=/home/fkhurshed/CLASS2, out=myfiles, maxdepth=1);


data erase;
set myfiles end = eof;

length str1 $1000.;

if _n_ =1 then call execute ('proc sql;');

if find( filename, 'sas7bdat')&amp;gt;0 then do;

datasetpath = catx('/', path, filename );

str1 = catx(' ', 'delete * from ', quote(trim(datasetpath)), ';');
call execute(str1);
end;

if eof then call execute ('quit;');


run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Drop all rows from the datasets. %Dirtree will create the list of files - use maxdepth to control how far down the subdirectories you navigate.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Mar 2023 19:15:09 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-03-28T19:15:09Z</dc:date>
    <item>
      <title>Keep only the first row on all the datasets in a directory and all of its subdirectories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-the-first-row-on-all-the-datasets-in-a-directory-and/m-p/866819#M342333</link>
      <description>&lt;P&gt;Hi everybody,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this task that I want to automate: I have a path containing multiple folders and subfolders. Each and every single one either contains a sas dataset or it is empty. I want to delete all the data in each one of these while keeping the structure of the table. For example if I have a table like the SASHELP.CLASS It will be converted to a table with zero rows but with all the metadata intact (SAS EG would display only Name Age Sex Weight and Height).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I could try to pipe some carefully crafted linux command to a file and then use each row for a libname statement, something like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;filename libs_file PIPE 'find /my/path -d'&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;then I could write:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;DATA libs;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; INPUT path 1000 $;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; INFILE libs_file;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; IF eof THEN CALL SYMPUT("nlibs", _N_);&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then use this to somehow use a LIBNAME for each row.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any input would be greatly appreciated.&lt;/P&gt;&lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2023 18:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-the-first-row-on-all-the-datasets-in-a-directory-and/m-p/866819#M342333</guid>
      <dc:creator>MikeFox</dc:creator>
      <dc:date>2023-03-28T18:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only the first row on all the datasets in a directory and all of its subdirectories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-the-first-row-on-all-the-datasets-in-a-directory-and/m-p/866829#M342335</link>
      <description>&lt;P&gt;Deleting data is scary, and crawling through a directory tree to delete all data is scarier...&amp;nbsp; but since you sound braver than me, I would use a directory tree crawler macro (or an OS command piped to a dataset) to build a control dataset which is a list of the SAS datasets of interest.&amp;nbsp; SAS can use a file specification in a DATA step, so you don't need a libname statement or a macro.&amp;nbsp; You could read in the control dataset and use CALL EXECUTE to generate a scary step for each dataset, like (untested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data "/path/to/mydata.sas7bdat";
  set "/path/to/mydata.sas7bdat";
  stop;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think that might work.&amp;nbsp; But I'm not going to test it.&amp;nbsp; And maybe take a backup first.&amp;nbsp; : )&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2023 18:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-the-first-row-on-all-the-datasets-in-a-directory-and/m-p/866829#M342335</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-03-28T18:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only the first row on all the datasets in a directory and all of its subdirectories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-the-first-row-on-all-the-datasets-in-a-directory-and/m-p/866846#M342344</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename dirtree url 'https://raw.githubusercontent.com/sasutils/macros/master/dirtree.sas';

%include dirtree;

%dirtree(directory=/home/fkhurshed/CLASS2, out=myfiles, maxdepth=1);


data erase;
set myfiles end = eof;

length str1 $1000.;

if _n_ =1 then call execute ('proc sql;');

if find( filename, 'sas7bdat')&amp;gt;0 then do;

datasetpath = catx('/', path, filename );

str1 = catx(' ', 'delete * from ', quote(trim(datasetpath)), ';');
call execute(str1);
end;

if eof then call execute ('quit;');


run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Drop all rows from the datasets. %Dirtree will create the list of files - use maxdepth to control how far down the subdirectories you navigate.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2023 19:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-the-first-row-on-all-the-datasets-in-a-directory-and/m-p/866846#M342344</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-03-28T19:15:09Z</dc:date>
    </item>
  </channel>
</rss>

