<?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: Looping a model over unique values of a variable and saving an output from each loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551214#M153130</link>
    <description>&lt;P&gt;Hi, I ran the following code without any error in the log this time:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_9_3uniq;
set data_9_3;
if c=1;
run;

proc sql noprint;
  select distinct t into :time from data_9_3uniq;
quit;

%macro dothis;
    %do i = 1 %to %sysfunc(countw(&amp;amp;time));
        %let thistime = %scan(&amp;amp;time,&amp;amp;i,%str( ));
		ods output FitStatistics=Fit; 
        proc phreg data=data_9_3;
             model t*c(0) = Z1 Z2 / ties=Breslow;
			 g_dum = g-1;
             if t &amp;gt; &amp;amp;thistime then do Z2=g_dum; Z1=0; end;
             else do Z2=0; Z1=g_dum; end;
         run;
         data outputdataset;
               set Fit;
               time=&amp;amp;thistime;
			   keep time Criterion WithCovariates;
			   if Criterion="-2 LOG L";
			   run;
         proc append base=all_outputs new=outputdataset;
         run;
    %end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But the likelihoods didn't append into &lt;CODE class=" language-sas"&gt;all_outputs&lt;/CODE&gt;.&amp;nbsp;It only shows me 1 row for %time=1. Maybe I'm making some mistake.&lt;/P&gt;</description>
    <pubDate>Mon, 15 Apr 2019 19:33:26 GMT</pubDate>
    <dc:creator>Blain</dc:creator>
    <dc:date>2019-04-15T19:33:26Z</dc:date>
    <item>
      <title>Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/550868#M152983</link>
      <description>&lt;P&gt;I'm trying to run a survival model where I first need to create a vector (named 'time' here) which contains all unique t given c=1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My final goal is to create a file or table where I can see the log likelihood for each value of t and find the t for which the log likelihood is maximum. My model is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc phreg data = data; 
	model t*c(0) = Z1 Z2 / ties=Breslow; 
	dum = g-1; 
	if t &amp;gt; time then do Z2=dum; Z1=0; end; 
	else do Z2=0; Z1=dum; end; 
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note that t=survival time, c=censoring status, g=group. I want to create a vector called 'time' which is the unique death times derived from t and c. Then I want to run the model for each value of 'time' and save the likelihood to create a table.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Apr 2019 01:42:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/550868#M152983</guid>
      <dc:creator>Blain</dc:creator>
      <dc:date>2019-04-14T01:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/550878#M152991</link>
      <description>&lt;P&gt;An outline of the solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
    select distinct t into :time separated by ' ' from data;
quit;

%macro dothis;
    %do i = 1 %to %sysfunc(countw(&amp;amp;time));
        %let thistime = %scan(&amp;amp;time,&amp;amp;i,%str( ));
        /* You will need to create an output data set from PROC PHREG */
        proc phreg data=data;
             ...
             if t &amp;gt; &amp;amp;thistime then do z2=dum; z1=0; end;
             ...
         run;
         data outputdataset;
               set outputdataset;
               time=&amp;amp;thistime;
         end;
         proc append base=all_outputs new=outputdataset;
         run;
    %end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 Apr 2019 12:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/550878#M152991</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-14T12:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/550965#M153027</link>
      <description>&lt;P&gt;Thanks for your reply but I think I'm still struggling to append the data sets. I was able to save the value of the log likelihood only for the first loop. I attach the data file here and the codes I tried (without the attempt to append as it would give an error) are as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_9_3uniq;
set data_9_3;
if c=1;
run;

proc sql noprint;
  select distinct t into :time from data_9_3uniq;
quit;

%macro dothis;
    %do i = 1 %to %sysfunc(countw(&amp;amp;time));
        %let thistime = %scan(&amp;amp;time,&amp;amp;i,%str( ));
		ods output FitStatistics=Fit; 
        proc phreg data=data_9_3;
             model t*c(0) = Z1 Z2 / ties=Breslow;
			 g_dum = g-1;
             if t &amp;gt; &amp;amp;thistime then do Z2=g_dum; Z1=0; end;
             else do Z2=0; Z1=g_dum; end;
         run;
         data outputdataset_&amp;amp;i;
               set Fit;
               time=&amp;amp;thistime;
			   keep time Criterion WithCovariates;
			   if Criterion="-2 LOG L";
		 run;
    %end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to append the results from&amp;nbsp;&lt;CODE class=" language-sas"&gt;outputdataset_&amp;amp;i&lt;/CODE&gt;&amp;nbsp;to&amp;nbsp;a single file. So, the final data set will have unique times (&lt;CODE class=" language-sas"&gt;&amp;amp;thistime&lt;/CODE&gt;) and the corresponding -2logLik or Lik.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 05:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/550965#M153027</guid>
      <dc:creator>Blain</dc:creator>
      <dc:date>2019-04-15T05:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/550993#M153032</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269100"&gt;@Blain&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your reply but I think I'm still struggling to append the data sets. I was able to save the value of the log likelihood only for the first loop. I attach the data file here and the codes I tried (without the attempt to append as it would give an error) are as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My example code uses PROC APPEND.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 10:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/550993#M153032</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-15T10:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551177#M153118</link>
      <description>&lt;P&gt;I think I need to use proc append as you suggested. But when I run the codes it gives me an error and the results do not append successfully. I'm quite inexperienced so couldn't reproduce. It will be great if you can make time and use the attached data to show an example. I just added the codes that ran before the proc append step in my previous reply.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 18:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551177#M153118</guid>
      <dc:creator>Blain</dc:creator>
      <dc:date>2019-04-15T18:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551181#M153119</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269100"&gt;@Blain&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;But when I run the codes it gives me an error and the results do not append successfully.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show us your code. Show us the LOG including the errors, by clicking on the {i} icon and pasting the log into that window.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 18:29:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551181#M153119</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-15T18:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551214#M153130</link>
      <description>&lt;P&gt;Hi, I ran the following code without any error in the log this time:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_9_3uniq;
set data_9_3;
if c=1;
run;

proc sql noprint;
  select distinct t into :time from data_9_3uniq;
quit;

%macro dothis;
    %do i = 1 %to %sysfunc(countw(&amp;amp;time));
        %let thistime = %scan(&amp;amp;time,&amp;amp;i,%str( ));
		ods output FitStatistics=Fit; 
        proc phreg data=data_9_3;
             model t*c(0) = Z1 Z2 / ties=Breslow;
			 g_dum = g-1;
             if t &amp;gt; &amp;amp;thistime then do Z2=g_dum; Z1=0; end;
             else do Z2=0; Z1=g_dum; end;
         run;
         data outputdataset;
               set Fit;
               time=&amp;amp;thistime;
			   keep time Criterion WithCovariates;
			   if Criterion="-2 LOG L";
			   run;
         proc append base=all_outputs new=outputdataset;
         run;
    %end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But the likelihoods didn't append into &lt;CODE class=" language-sas"&gt;all_outputs&lt;/CODE&gt;.&amp;nbsp;It only shows me 1 row for %time=1. Maybe I'm making some mistake.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 19:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551214#M153130</guid>
      <dc:creator>Blain</dc:creator>
      <dc:date>2019-04-15T19:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551221#M153133</link>
      <description>&lt;P&gt;As a diagnostic tool, you need to actually look at your data sets, with your own eyes, and see what is in them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, since I can't look at YOUR data sets, please see how many observations are in data_9_3uniq. Is there 1 observation? Is there 1 distinct value of t?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 19:40:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551221#M153133</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-15T19:40:58Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551227#M153138</link>
      <description>There are 80 unique t in my data set. So, time should have 80 unique values. I attached the data set for your reference.&lt;BR /&gt;</description>
      <pubDate>Mon, 15 Apr 2019 19:52:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551227#M153138</guid>
      <dc:creator>Blain</dc:creator>
      <dc:date>2019-04-15T19:52:18Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551232#M153140</link>
      <description>&lt;P&gt;Okay, I see the error, please go back to my original example code where it says:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
    select distinct t into :time separated by ' ' from data;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That's what you missed.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 19:58:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551232#M153140</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-15T19:58:09Z</dc:date>
    </item>
    <item>
      <title>Re: Looping a model over unique values of a variable and saving an output from each loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551237#M153144</link>
      <description>&lt;P&gt;Thank you so much for your help!&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 20:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-a-model-over-unique-values-of-a-variable-and-saving-an/m-p/551237#M153144</guid>
      <dc:creator>Blain</dc:creator>
      <dc:date>2019-04-15T20:09:22Z</dc:date>
    </item>
  </channel>
</rss>

