title: Outputting to a dataset while in a macro do loop
body: <p>Here's a macro I'm writing, I want to look at a given data set, and write of each of the field(column) names as an observation into an info data set. </p>
  
  <p>Here's what I've got</p>
  
  <pre><code>%macro go_macro(data);
  
  
  /*get the list of field names from the dictionary*/
  proc sql noprint;
  
      select distinct name into :columns separated by ' '
      from dictionary.columns
      where memname = "%upcase(%scan(&amp;data, -1))" and libname = "%upcase(%scan(&amp;data, 1))" and type = "char"
  ;
  
  quit;
  
  /*now loop through this list of fieldnames*/
  %let var_no = 1; 
  %let var = %scan(&amp;columns, &amp;var_no, ' '); 
  
  %do %while (&amp;var ne);
  
                   /*here is where I want to write the fieldname to an observation*/
      fname = "&amp;var"; 
                   output; 
  
  
  %let var_no = %eval(&amp;var_no +1);
  %let var = %scan(&amp;columns, &amp;var_no, ' '); 
  %end;
  
  %mend;
  
  
  /*execute the code*/
  data bdqa.accounts_info; 
  %go_macro(braw.accounts)
  run;
  </code></pre>
  
  <p>this gives me </p>
  
  <pre><code>[MPRINT] Parsing Base DataServer
  /* 0005 */      fname = "SORT_CODE"; 
  /* 0006 */          output; 
  /* 0009 */      fname = "BANK_NAME"; 
  /* 0010 */          output; 
  /* 0013 */      fname = "CREDIT_CARD"; 
  /* 0014 */          output; 
  /* 0017 */      fname = "PRIMARY_ACCT_HOLDER"; 
  /* 0018 */          output; 
  /* 0021 */      fname = "account_close_date"; 
  /* 0022 */          output;
  /* 0023 */      run;
  
  
      ERROR: Parsing exception - aborting
  ERROR: DS-00274 : Could not parse base DataServer code: Encountered " &lt;ALPHANUM&gt; "fname "" at line 5, column 9.
  Was expecting one of:
      &lt;EOF&gt; 
      ";" ...
      "*" ...
      "data" ...
      "proc" ...
      (and 9 more)
      while
  </code></pre>
  
  <p>while</p>
  
  <pre><code>data mytest;
  
      do i = 1 to 5; 
      fname = 'hello world';
      output; 
      end;
      keep fname; 
  run;
  </code></pre>
  
  <p>is perfectly legit. </p>
  
  <p>the following code </p>
  
  <pre><code>%macro char_freqs(data=);
  
  /*get the different variables*/
  proc sql noprint;
  
  select distinct name into :columns separated by ' '
  from dictionary.columns
  where memname = "%upcase(%scan(&amp;data, -1))" and libname = "%upcase(%scan(&amp;data, 1))" and type = "char"
  ;
  
  quit;
  
  /*now get the distinct values for each of the variables*/
  %let var_no = 1; 
  %let var = %scan(&amp;columns, &amp;var_no, ' '); 
  
  %do %while (&amp;var ne);
  
      proc freq data=&amp;data; 
      tables &amp;var/out=&amp;var._freq; 
      run;    
  
  %let var_no = %eval(&amp;var_no +1);
  %let var = %scan(&amp;columns, &amp;var_no, ' '); 
  %end;
  
  %mend;
  
  
  %char_freqs(data=macros.businesses)
  </code></pre>
  
  <p>Also works - the PROC FREQ is allowed. </p>