<?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: output data from macro to text file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644123#M192354</link>
    <description>&lt;P&gt;Thanks.&amp;nbsp; &amp;nbsp;This bypasses the macro necessity and make things a lot simpler.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 30 Apr 2020 00:43:44 GMT</pubDate>
    <dc:creator>LB</dc:creator>
    <dc:date>2020-04-30T00:43:44Z</dc:date>
    <item>
      <title>output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644082#M192331</link>
      <description>&lt;P&gt;So I am a little rusty on macros -- Below two things happen -first I output a dataset with dates (Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp; - I took this from you) . The next part I am using the dataset to attempt to output to a text file-&lt;/P&gt;
&lt;P&gt;Once I figure out how to do it, I can expand on what I need to do-for now&amp;nbsp; I just can't figure how to successfully capture the observations - TIA.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data arghh;
Do year=2013 to 2016;
Do month=1 to 12;
n='N'||COMPRESS(PUT(MONTH,8.)||PUT(YEAR,8.));
Date1=mdy(month, 1, year);
date2=intnx('month',date1,0,'end');
date1T=put(date1,ddmmyy.);
date2T=put(date2,ddmmyy.);
Output;
End;
End;
format date1 date9. date2 date9.;
run;

%macro roller(data=, var=,var1=,var2=);
   proc sort data=&amp;amp;data(keep=&amp;amp;var &amp;amp;var1 &amp;amp;var2) out=values nodupkey;
    by &amp;amp;var  ;
   run;
   data _null_;
   file 'test2.txt';
      set values end=last;
      call symputx('siteA'||left(_n_),&amp;amp;var);
	  call symputx('DateA'||left(_n_),&amp;amp;var1);
	  call symputx('DateB'||left(_n_),&amp;amp;var2);
      if last then call symputx('countx',_n_,'g');
   run;

select(&amp;amp;var);


%do i=1 %to &amp;amp;countx; 
%PUT "A &amp;amp;siteA&amp;amp;i";
%PUT "A &amp;amp;&amp;amp;DateA&amp;amp;i";
%PUT "B &amp;amp;&amp;amp;DateB&amp;amp;i";

run;
 file print; /*doesn't seem to work here*/ 
 %END;

 %MEND roller;

%roller(data=arghh, var=N,var1=date1T,var2=date2T);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 20:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644082#M192331</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2020-04-29T20:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644091#M192337</link>
      <description>&lt;P&gt;If you mean the values of macro variables you have two basic choices.&lt;/P&gt;
&lt;P&gt;Best is likely to place the values into a data set then all of the Proc Print/Tabulate/Report or Put options in a data step are available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or us %put to write the values to the Log while using Proc Printto to send Log output to file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run; ends a data step. So code after that is outside of a valid data step and throws errors. Your Select, when needs and End statement, would have to be before the first run, as would the File Print; Plus it is not clear what the select is supposed to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Due to timing of code execution vs compilation then use of macro statements such as %put inside a data step seldom does what is desired.&lt;/P&gt;
&lt;P&gt;Dummy code of what I think you may have intended:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data _null_&lt;/P&gt;
&lt;P&gt;&amp;lt;create macro vars&amp;gt;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;Proc printto&amp;nbsp; log='path\outputfile.txt';&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%do i=1 %to &amp;amp;countx; &lt;BR /&gt;%PUT "A &amp;amp;siteA&amp;amp;i";&lt;BR /&gt;%PUT "A &amp;amp;&amp;amp;DateA&amp;amp;i";&lt;BR /&gt;%PUT "B &amp;amp;&amp;amp;DateB&amp;amp;i";&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;/* stop sendit %put to file*/&lt;/P&gt;
&lt;P&gt;proc printto;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;lt;end the macro&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And generally any code that places lots of data step values into macro variables needs to be reconsidered. Just how are you going to use all of those site and date macro variables?&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 21:46:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644091#M192337</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-29T21:46:43Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644093#M192338</link>
      <description>I had thought about this but reading back in file and data clean would be a pain- In short what I will be doing and what I have done in the past (if i could just remember what I did) was to essentially export a text file as a SAS program for the purposes of executing it on the SAS Grid at the same time.  I know there is a way to do this as I have done in the past-just don't have access to the code...&lt;BR /&gt;</description>
      <pubDate>Wed, 29 Apr 2020 21:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644093#M192338</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2020-04-29T21:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644098#M192339</link>
      <description>&lt;P&gt;It's not quite clear to me what you want your text file to look like.&amp;nbsp; Can you provide a sample of what you expect your text file to contain?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your %put statements write out something almost like this.&amp;nbsp; Is this what should be in your text file?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"A N102013"
"A 01/10/13"
"B 31/10/13"
"A N102014"
"A 01/10/14"
"B 31/10/14"&lt;BR /&gt;...&lt;/CODE&gt;&lt;/PRE&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>Wed, 29 Apr 2020 22:09:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644098#M192339</guid>
      <dc:creator>JerryV</dc:creator>
      <dc:date>2020-04-29T22:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644099#M192340</link>
      <description>&lt;P&gt;Create a string value in a data step. Put the value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
   file "&amp;lt;path&amp;gt;\output.txt";
   Put "some literal text to write to the file";
   Put "some other text";
   /* if the value is in data step variables*/
   put "the value for variable x is " x; /*&amp;lt;= may need to specify a format for x*/
run;&lt;/PRE&gt;
&lt;P&gt;I didn't see anything that looked like an attempt to write instructions, just placing a bunch of data step value into macro variables.&lt;/P&gt;
&lt;P&gt;If you need multiple variables on the same line in the text they go in the same PUT statement. If you need to control columns that things output to then the pointer controls like +1 (to move the pointer to the right 1 space) or &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/54795"&gt;@25&lt;/a&gt; to print a value at column 25 in the text file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hint:&lt;/P&gt;
&lt;P&gt;Show a short example of the data you have and then an example of what the text file should look like.&lt;/P&gt;
&lt;P&gt;Paste the example text file into a code box opened on the forum with the &amp;lt;/&amp;gt; or the results are likely to get reformatted and not actually be what was intended.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 22:10:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644099#M192340</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-29T22:10:32Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644109#M192347</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LB_0-1588200257878.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38895i509564A09205CA82/image-size/medium?v=v2&amp;amp;px=400" role="button" title="LB_0-1588200257878.png" alt="LB_0-1588200257878.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13878"&gt;@JerryV&lt;/a&gt;&amp;nbsp; - This is what the text will look like generally and roughly when outputted but you get the idea- I need to fix it so that it will export out the ; at the end.&amp;nbsp; Using a file statement&amp;nbsp; within the macro and put statements without the % results in the macros not being invoked. I changed the code so it is mostly there- I will prob go with the proc printto concept and then can clean up the file and re-export to a SAS file and execute.&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;PRE&gt;%macro roller(data=, var=,var1=,var2=);
   proc sort data=&amp;amp;data(keep=&amp;amp;var &amp;amp;var1 &amp;amp;var2) out=values nodupkey;
    by &amp;amp;var  ;
   run;
   data _null_;
  
      set values end=last;
      call symputx('siteA'||left(_n_),&amp;amp;var);
	  call symputx('DateA'||left(_n_),&amp;amp;var1);
	  call symputx('DateB'||left(_n_),&amp;amp;var2);
      if last then call symputx('countx',_n_,'g');
   run;

data _null_;
 Proc printto  log='outputfile.txt';

run;

%do i=1 %to &amp;amp;countx; 
%PUT signon  &amp;amp;&amp;amp;siteA&amp;amp;i;
%PUT rsubmit  &amp;amp;&amp;amp;siteA&amp;amp;i;

%PUT  proc sql (sysfunc here for ';'); 
%PUT SELECT * FROM BLAH WHERE XYZ between "&amp;amp;&amp;amp;DateA&amp;amp;i" and "&amp;amp;&amp;amp;DateB&amp;amp;i"; 
%put Quit';';
%put endrsubmit;

run;


 %END;
proc printto;
 %MEND roller;

%roller(data=arghh, var=N,var1=date1T,var2=date2T);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 22:51:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644109#M192347</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2020-04-29T22:51:11Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644114#M192350</link>
      <description>&lt;P&gt;There is no need to move the data into macro variables. Just use a data step to write the file. Use the PUT statement.&lt;/P&gt;
&lt;P&gt;If you want the data step to write multiple files use the FILENAME= option on the FILE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
   file code;
   set mydata ;
   put 'signon ' siteA ';'
     / 'rsubmit ' siteA&amp;nbsp;';'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/&amp;nbsp;'proc&amp;nbsp;sql;'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/&amp;nbsp;'select&amp;nbsp;*&amp;nbsp;from&amp;nbsp;BLAH&amp;nbsp;where&amp;nbsp;XYZ&amp;nbsp;between&amp;nbsp;'''&amp;nbsp;dataA&amp;nbsp;'''&amp;nbsp;and&amp;nbsp;'''&amp;nbsp;dateb&amp;nbsp;''';'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/&amp;nbsp;'quit;'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/&amp;nbsp;'endrsubmit;'
&amp;nbsp;&amp;nbsp;;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have a bunch of commands for the same site you might want to sort the dataset by SITEA and use FIRST. and LAST. processing to generate the code all together.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 23:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644114#M192350</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-29T23:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644122#M192353</link>
      <description>&lt;P&gt;I will suggest again:&lt;/P&gt;
&lt;P&gt;Show the values &lt;STRONG&gt;in the data set&lt;/STRONG&gt; and &lt;STRONG&gt;the exact lines of text&lt;/STRONG&gt; that are supposed to go into the text file.&lt;/P&gt;
&lt;P&gt;Having stuff like " (sysfunc here for ';') " does not show what text you need to generate. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that picture is incomplete because RSUBMIT and ENDRSUBMIT are likley missing semicolons as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may also not be familiar with the data step CALL EXECUTE statement. You can use a data step with an input data set to contain changing values and create lines of code to execute. The statements get put into the execution stack and start after the data step completes.&lt;/P&gt;
&lt;P&gt;A very simple example with one record in the a data set:&lt;/P&gt;
&lt;PRE&gt;data example;
   dsname ='sashelp.class';
run;

data _null_;
   set example;
   Call execute ("proc print data=");
   call execute (dsname);
   call execute ("; run;");
run;&lt;/PRE&gt;
&lt;P&gt;You can see that the first and third call execute have basic "boiler plate" or static instructions. The variable value gets stuck in after the data=. This is what the log would look like:&lt;/P&gt;
&lt;PRE&gt;1   + proc print data=
2   + sashelp.class
3   + ; run;
&lt;/PRE&gt;
&lt;P&gt;so you can see the code generated and the results.&lt;/P&gt;
&lt;P&gt;But the idea is the same. We have to know the &lt;STRONG&gt;exact&lt;/STRONG&gt; text that would be generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Apr 2020 00:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644122#M192353</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-30T00:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644123#M192354</link>
      <description>&lt;P&gt;Thanks.&amp;nbsp; &amp;nbsp;This bypasses the macro necessity and make things a lot simpler.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Apr 2020 00:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644123#M192354</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2020-04-30T00:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: output data from macro to text file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644124#M192355</link>
      <description>Thank you -also very useful to know for future reference.  &lt;BR /&gt;</description>
      <pubDate>Thu, 30 Apr 2020 00:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-data-from-macro-to-text-file/m-p/644124#M192355</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2020-04-30T00:45:15Z</dc:date>
    </item>
  </channel>
</rss>

