<?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 What are the ways of repeating a SAS program many times, by specifying inputs &amp;amp; outputs each time? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/What-are-the-ways-of-repeating-a-SAS-program-many-times-by/m-p/590237#M168887</link>
    <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I am a new user of SAS who has only worked with R previously. My goal is to generate many sets of large simulations. I have written some codes for running one set of simulations, which are shown below. My questions are:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;1. What is the best way to repeat these codes for x number of times automatically, and&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;2. how to specify a range of inputs and outputs for each time the codes are excuted? (e.g. importing files named '&lt;CODE class=" language-sas"&gt;design_matrix_1.csv', 'design_matrix_2.csv', ...., 'design_matrix_10.csv', and exporting the results as 'result1_1.csv', ..., 'result1_10.csv', respectively)&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One way I can think of is to create a macro, and manually specify the input and output files for each scenario.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N = 10000;

* import design matrix
PROC IMPORT DATAFILE='/folders/myfolders/design_matrix_1.csv' replace
	OUT=design;
	GETNAMES=YES;
RUN;

* extract information from design matrix
proc sql noprint;
select max(Clus) into : nclus from design;
quit;

* generate N copies of the design matrix
data design_rep;
   do i = 1 to &amp;amp;N;
      do j = 1 to n;
         set design nobs=n point=j;
         output;
         end;
      end;
   stop;
   run;

* generate random numbers to simulate datasets
data sim;
set design_rep ;
    by i Clus;
	retain clus_int;
	if first.Clus then do;
	clus_int = rand('normal', 0, sigma_clus_lp);
	end;
error = rand("Normal", 0, &amp;amp;sigma);
y = Tx* &amp;amp;Tx_effect + period + clus_int + error;
run;

ods exclude all;
ods noresults;

* analyze data
PROC glimmix data=sim;
by i;
CLASS Clus ;
MODEL y = Tx Period /S  DDFM=KR;
RANDOM intercept / subject = Clus ;
NLOPTIONS TECHNIQUE=DBLDOG;
ods output Tests3 = result1 ParameterEstimates = result2;
RUN;

ods exclude none;
ods results;

PROC EXPORT
DATA=result1
replace
OUTFILE="/folders/myfolders/result_1.csv"
DBMS=CSV;

PROC EXPORT
DATA=result2
replace
OUTFILE="/folders/myfolders/result_2.csv"
DBMS=CSV;

data reject (keep = Reject); 
   set result1; 
   where Effect = 'Tx';
   Reject = (ProbF &amp;lt; 0.05); 
run; 

proc summary data = reject;
var Reject;
output out = power(drop = _FREQ_ _TYPE_ ) mean = ;
run;

PROC PRINT DATA=power;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Sep 2019 23:40:40 GMT</pubDate>
    <dc:creator>AnOrangeTree</dc:creator>
    <dc:date>2019-09-19T23:40:40Z</dc:date>
    <item>
      <title>What are the ways of repeating a SAS program many times, by specifying inputs &amp; outputs each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-are-the-ways-of-repeating-a-SAS-program-many-times-by/m-p/590237#M168887</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I am a new user of SAS who has only worked with R previously. My goal is to generate many sets of large simulations. I have written some codes for running one set of simulations, which are shown below. My questions are:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;1. What is the best way to repeat these codes for x number of times automatically, and&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;2. how to specify a range of inputs and outputs for each time the codes are excuted? (e.g. importing files named '&lt;CODE class=" language-sas"&gt;design_matrix_1.csv', 'design_matrix_2.csv', ...., 'design_matrix_10.csv', and exporting the results as 'result1_1.csv', ..., 'result1_10.csv', respectively)&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One way I can think of is to create a macro, and manually specify the input and output files for each scenario.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N = 10000;

* import design matrix
PROC IMPORT DATAFILE='/folders/myfolders/design_matrix_1.csv' replace
	OUT=design;
	GETNAMES=YES;
RUN;

* extract information from design matrix
proc sql noprint;
select max(Clus) into : nclus from design;
quit;

* generate N copies of the design matrix
data design_rep;
   do i = 1 to &amp;amp;N;
      do j = 1 to n;
         set design nobs=n point=j;
         output;
         end;
      end;
   stop;
   run;

* generate random numbers to simulate datasets
data sim;
set design_rep ;
    by i Clus;
	retain clus_int;
	if first.Clus then do;
	clus_int = rand('normal', 0, sigma_clus_lp);
	end;
error = rand("Normal", 0, &amp;amp;sigma);
y = Tx* &amp;amp;Tx_effect + period + clus_int + error;
run;

ods exclude all;
ods noresults;

* analyze data
PROC glimmix data=sim;
by i;
CLASS Clus ;
MODEL y = Tx Period /S  DDFM=KR;
RANDOM intercept / subject = Clus ;
NLOPTIONS TECHNIQUE=DBLDOG;
ods output Tests3 = result1 ParameterEstimates = result2;
RUN;

ods exclude none;
ods results;

PROC EXPORT
DATA=result1
replace
OUTFILE="/folders/myfolders/result_1.csv"
DBMS=CSV;

PROC EXPORT
DATA=result2
replace
OUTFILE="/folders/myfolders/result_2.csv"
DBMS=CSV;

data reject (keep = Reject); 
   set result1; 
   where Effect = 'Tx';
   Reject = (ProbF &amp;lt; 0.05); 
run; 

proc summary data = reject;
var Reject;
output out = power(drop = _FREQ_ _TYPE_ ) mean = ;
run;

PROC PRINT DATA=power;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 23:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-are-the-ways-of-repeating-a-SAS-program-many-times-by/m-p/590237#M168887</guid>
      <dc:creator>AnOrangeTree</dc:creator>
      <dc:date>2019-09-19T23:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: What are the ways of repeating a SAS program many times, by specifying inputs &amp; outputs each</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-are-the-ways-of-repeating-a-SAS-program-many-times-by/m-p/590238#M168888</link>
      <description>&lt;P&gt;Macros, Call Execute or DOBSUBL are the usual options.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;UCLA introductory tutorial on macro variables and macros&lt;/H2&gt;
&lt;P&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" rel="nofollow" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;&lt;A id="user-content-tutorial-on-converting-a-working-program-to-a-macro" class="anchor" href="https://github.com/statgeek/SAS-Tutorials#tutorial-on-converting-a-working-program-to-a-macro" aria-hidden="true" target="_blank"&gt;&lt;/A&gt;Tutorial on converting a working program to a macro&lt;/H2&gt;
&lt;P&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro Appendix with examples:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290382"&gt;@AnOrangeTree&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I am a new user of SAS who has only worked with R previously. My goal is to generate many sets of large simulations. I have written some codes for running one set of simulations, which are shown below. My questions are:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;1. What is the best way to repeat these codes for x number of times automatically, and&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;2. how to specify a range of inputs and outputs for each time the codes are excuted? (e.g. importing files named '&lt;CODE class=" language-sas"&gt;design_matrix_1.csv', 'design_matrix_2.csv', ...., 'design_matrix_10.csv', and exporting the results as 'result1_1.csv', ..., 'result1_10.csv', respectively)&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way I can think of is to create a macro, and manually specify the input and output files for each scenario.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N = 10000;

* import design matrix
PROC IMPORT DATAFILE='/folders/myfolders/design_matrix_1.csv' replace
	OUT=design;
	GETNAMES=YES;
RUN;

* extract information from design matrix
proc sql noprint;
select max(Clus) into : nclus from design;
quit;

* generate N copies of the design matrix
data design_rep;
   do i = 1 to &amp;amp;N;
      do j = 1 to n;
         set design nobs=n point=j;
         output;
         end;
      end;
   stop;
   run;

* generate random numbers to simulate datasets
data sim;
set design_rep ;
    by i Clus;
	retain clus_int;
	if first.Clus then do;
	clus_int = rand('normal', 0, sigma_clus_lp);
	end;
error = rand("Normal", 0, &amp;amp;sigma);
y = Tx* &amp;amp;Tx_effect + period + clus_int + error;
run;

ods exclude all;
ods noresults;

* analyze data
PROC glimmix data=sim;
by i;
CLASS Clus ;
MODEL y = Tx Period /S  DDFM=KR;
RANDOM intercept / subject = Clus ;
NLOPTIONS TECHNIQUE=DBLDOG;
ods output Tests3 = result1 ParameterEstimates = result2;
RUN;

ods exclude none;
ods results;

PROC EXPORT
DATA=result1
replace
OUTFILE="/folders/myfolders/result_1.csv"
DBMS=CSV;

PROC EXPORT
DATA=result2
replace
OUTFILE="/folders/myfolders/result_2.csv"
DBMS=CSV;

data reject (keep = Reject); 
   set result1; 
   where Effect = 'Tx';
   Reject = (ProbF &amp;lt; 0.05); 
run; 

proc summary data = reject;
var Reject;
output out = power(drop = _FREQ_ _TYPE_ ) mean = ;
run;

PROC PRINT DATA=power;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 00:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-are-the-ways-of-repeating-a-SAS-program-many-times-by/m-p/590238#M168888</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-20T00:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: What are the ways of repeating a SAS program many times, by specifying inputs &amp; outputs each</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-are-the-ways-of-repeating-a-SAS-program-many-times-by/m-p/590537#M169031</link>
      <description>&lt;P&gt;Thanks for sharing these resources! They're really helpful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 19:23:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-are-the-ways-of-repeating-a-SAS-program-many-times-by/m-p/590537#M169031</guid>
      <dc:creator>AnOrangeTree</dc:creator>
      <dc:date>2019-09-20T19:23:07Z</dc:date>
    </item>
  </channel>
</rss>

