<?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: Can we use macro to check how many variables the files have (With pipe) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350212#M81384</link>
    <description>&lt;P&gt;So you can read the files and figure out how many columns each one has.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist ;
  length fname filename $255 memname $32 ;
  infile "&amp;amp;path\*.txt" dsd truncover filename=fname end=eof column=col length=len;
  input @;
  do n=1 by 1 until (col &amp;gt; len) ;
    input word :$50. @;
  end;
  input;
  if fname ne lag(fname) then do ;
    if _n_ &amp;gt; 1 then output;
    filename = fname ;
    memname = scan(scan(filename,-1,'/\'),1,'.');
    maxn=n ;
  end;
  else maxn=max(maxn,n);
  if eof then output;
  retain maxn filename memname ;
  keep filename memname maxn ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then if you want you can push all of that data into multiple macro variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if eof then call symputx('nfiles',_n_-1);
  set filelist end=eof;
  call symputx(cats('read',_n_),filename);
  call symputx(cats('dset',_n_),memname); 
  call symputx(cats('n',_n_),maxn);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that you could make a macro that loops over all of the files and generates code to read each one into a dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro readin;
%do i=1 %to &amp;amp;nfiles;
data &amp;amp;&amp;amp;dset&amp;amp;i;
  infile "&amp;amp;&amp;amp;read&amp;amp;i" truncover dsd;
  input (var1-var&amp;amp;&amp;amp;n&amp;amp;i) (:$10.) ;
run;
%end;
%mend readin;

%readin;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why not just generate the code directly from the data instead of making all of those macro variables and having to create a macro?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set filelist ;
  file code ;
  put 'data ' memname ';' 
    / '  infile ' filename :$quote. 'truncover dsd;'
    / '  input (var1-var' maxn ') (:$10.);'
    / 'run;'
  ;
run;
%include code / source2 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 15 Apr 2017 03:49:00 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-04-15T03:49:00Z</dc:date>
    <item>
      <title>Can we use macro to check how many variables the files have (With pipe)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350143#M81355</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;C:\ab\cd\filename DIRLIST pipe 'dir "C:\ab\cd\*.txt" /b ';

data dirlist ;
   infile dirlist lrecl=200 truncover;
   input file_name $100.;
run;
&lt;BR /&gt;****Part I; old file;

data _null_;
   set dirlist end=end;
   count+1;
   call symputx('read'||put(count,4.-l),cats('C:\ab\cd\',file_name));
   call symputx('dset'||put(count,4.-l),scan(file_name,1,'.'));
   if end then call symputx('max',count);
run;

options mprint symbolgen;
%macro readin;
   %do i=1 %to &amp;amp;max;

      data &amp;amp;&amp;amp;dset&amp;amp;i;
         infile "&amp;amp;&amp;amp;read&amp;amp;i" lrecl=1000 truncover dsd;
         input var1 $ var2 $ var3 $;
       run;

   %end;
%mend readin;

%readin;

The limitation is that it can show limited amount of variables. If I am not sure how many variables they have, I want to use another loop.
options mprint symbolgen;&lt;BR /&gt;&lt;BR /&gt;******part II, new one; I want to replace the part I with code below by adding &amp;amp;j so that I need not know how many variables each file has, need not type var1, var2, var3, etc;&lt;BR /&gt;&lt;BR /&gt;
%macro readin;
   %do i=1 %to &amp;amp;max;
     %do j=1 %to &amp;amp;max;

      data &amp;amp;&amp;amp;dset&amp;amp;i;
         infile "&amp;amp;&amp;amp;read&amp;amp;i" lrecl=1000 truncover dsd;
         input var&amp;amp;j $ 
       run;

   %end;
  %end;
%mend readin;

%readin;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Apr 2017 20:18:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350143#M81355</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2017-04-14T20:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: Can use macro to check how many variables the files have</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350145#M81356</link>
      <description>&lt;P&gt;I don't know what your question is.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 18:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350145#M81356</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-14T18:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: Can use macro to check how many variables the files have</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350157#M81359</link>
      <description>&lt;P&gt;If you don't know what is your text files you may want to look at PROC IMPORT to bring data into SAS.&lt;/P&gt;
&lt;P&gt;Set a largish value for the guessingrows option.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 18:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350157#M81359</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-04-14T18:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Can use macro to check how many variables the files have</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350165#M81362</link>
      <description>&lt;P&gt;sorry I have not made it clear&lt;/P&gt;
&lt;P&gt;I want to replace my old sas program with new one, by introducing another macro &amp;amp;j&lt;/P&gt;
&lt;P&gt;other wise, you can see from my previous sas program, i typed, var1 var2 var3, I want to replace this with var&amp;amp;j, but it does not work as I expect. so i hope you can check my second part of my sas code and give advice&lt;/P&gt;
&lt;P&gt;thank you&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 19:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350165#M81362</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2017-04-14T19:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: Can use macro to check how many variables the files have</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350173#M81366</link>
      <description>&lt;P&gt;Do you want to read all of the txt files in a directory? &amp;nbsp;If so there is no need for macro varaibles to do this. Are you just counting how many columns are in each txt file? Can we assume that the each row has the same number of fields? Or do you want to find the maximum over the whole file? &amp;nbsp;So perhaps something like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  length fname filename $255 ;
  infile "C:\ab\cd\*.txt" dsd truncover filename=fname end=eof column=col length=len;
  input @;
  do n=1 by 1 while (col &amp;lt;= len) ;
    input word :$50. @;
  end;
  input;
  if fname ne lag(fname) then do ;
    if _n_ &amp;gt; 1 then output;
    filename = fname ;
    maxn=n ;
  end;
  else maxn=max(maxn,n);
  if eof then output;
  retain maxn filename ;
  keep filename maxn ;
run;
    &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Apr 2017 19:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350173#M81366</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-14T19:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: Can use macro to check how many variables the files have</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350178#M81368</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR 161-185: No matching DO/SELECT statement.

ERROR 160-185: No matching IF-THEN clause.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;EM&gt; I got error messages with you code.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;What I want to do is I want to replace my part I code with my part II code, by add one more macro &amp;amp;j. It does not work. I am open to other options as well. Thanks.&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 19:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350178#M81368</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2017-04-14T19:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: Can use macro to check how many variables the files have</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350185#M81373</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40773"&gt;@Bal23&lt;/a&gt;&amp;nbsp;When editing your original post please indicate what's changed. Please remember that although people are trying to help you answer your question, this is also to create a body of knowledge that other users can use in the future to help answer their question.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Changing your original post after answers have been posted confuse this especially since there's no history for a post to trace edits.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 20:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350185#M81373</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-14T20:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Can use macro to check how many variables the files have</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350188#M81374</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I added comments, marked one as part i and the other as part II. I do not change any code. I wanted to make it clear and did not intend to confuse those who want to help. Thank you and sorry that confuses you.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 20:36:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350188#M81374</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2017-04-14T20:36:32Z</dc:date>
    </item>
    <item>
      <title>Re: Can use macro to check how many variables the files have</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350199#M81379</link>
      <description>&lt;P&gt;Your current program is saying for the 5th file you should read one variable and name it VAR5.&lt;/P&gt;
&lt;P&gt;Is that what you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would assume that what you want to is have a series of number N1, N2,.... and so for the Jth file you would want to read variables VAR1 to VAR&amp;amp;&amp;amp;n&amp;amp;j.&lt;/P&gt;
&lt;P&gt;How do you intend find out these N1, N2, ... numbers?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 21:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350199#M81379</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-14T21:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use macro to check how many variables the files have (With pipe)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350212#M81384</link>
      <description>&lt;P&gt;So you can read the files and figure out how many columns each one has.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist ;
  length fname filename $255 memname $32 ;
  infile "&amp;amp;path\*.txt" dsd truncover filename=fname end=eof column=col length=len;
  input @;
  do n=1 by 1 until (col &amp;gt; len) ;
    input word :$50. @;
  end;
  input;
  if fname ne lag(fname) then do ;
    if _n_ &amp;gt; 1 then output;
    filename = fname ;
    memname = scan(scan(filename,-1,'/\'),1,'.');
    maxn=n ;
  end;
  else maxn=max(maxn,n);
  if eof then output;
  retain maxn filename memname ;
  keep filename memname maxn ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then if you want you can push all of that data into multiple macro variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if eof then call symputx('nfiles',_n_-1);
  set filelist end=eof;
  call symputx(cats('read',_n_),filename);
  call symputx(cats('dset',_n_),memname); 
  call symputx(cats('n',_n_),maxn);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that you could make a macro that loops over all of the files and generates code to read each one into a dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro readin;
%do i=1 %to &amp;amp;nfiles;
data &amp;amp;&amp;amp;dset&amp;amp;i;
  infile "&amp;amp;&amp;amp;read&amp;amp;i" truncover dsd;
  input (var1-var&amp;amp;&amp;amp;n&amp;amp;i) (:$10.) ;
run;
%end;
%mend readin;

%readin;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why not just generate the code directly from the data instead of making all of those macro variables and having to create a macro?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set filelist ;
  file code ;
  put 'data ' memname ';' 
    / '  infile ' filename :$quote. 'truncover dsd;'
    / '  input (var1-var' maxn ') (:$10.);'
    / 'run;'
  ;
run;
%include code / source2 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Apr 2017 03:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-we-use-macro-to-check-how-many-variables-the-files-have-With/m-p/350212#M81384</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-15T03:49:00Z</dc:date>
    </item>
  </channel>
</rss>

