<?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: SAS IML: Simulating continuous and binary variable with a time step index using proc IML in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528223#M4512</link>
    <description>&lt;P&gt;Thank you Rick,&lt;/P&gt;&lt;P&gt;I wanted to be able to simulate future whz[t] and pa[t] as a function of past whz[t-1]&amp;nbsp; and pa[t-1], respectively. In other words, I wanted to get data similar to this data step simulation but I'd like to do it in SAS IML.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start=1;
%let endpoint=10;
%let grandmean=20;
%let std=1;
data simregwide;
      array arwhz	{&amp;amp;start:&amp;amp;endpoint} whz&amp;amp;start-whz&amp;amp;endpoint;
	  array arpa	{&amp;amp;start:&amp;amp;endpoint} pa&amp;amp;start-pa&amp;amp;endpoint;
	  
		call streaminit(1234);
		do id=1 to 5;
			do t = &amp;amp;start to &amp;amp;endpoint;
			  if t =1 then
					do;
					   arwhz[1]=0.2;
					   arpa[1]=0;
					end;

				else
					do;
					    arwhz[t]=rand("normal", &amp;amp;grandmean + arwhz[t-1], &amp;amp;std);
			  			arpa[t]=rand("bernouilli", logistic(2*arpa[t-1]));
			        end;
			
			end;
    		output;
		end;
		keep id whz1-whz10 pa1-pa10;
run;

Data simreglong ;
retain id t whz pa;
	set simregwide;
	  array arwhz	{&amp;amp;start:&amp;amp;endpoint} whz&amp;amp;start-whz&amp;amp;endpoint;
	  array arpa	{&amp;amp;start:&amp;amp;endpoint} pa&amp;amp;start-pa&amp;amp;endpoint;

	do t = &amp;amp;start to &amp;amp;endpoint;
			whz =	arwhz[t];
			pa = 	arpa[t];
	output;
	end;

	keep 	id t whz pa;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Thank you&lt;/P&gt;</description>
    <pubDate>Fri, 18 Jan 2019 01:13:36 GMT</pubDate>
    <dc:creator>SimRock</dc:creator>
    <dc:date>2019-01-18T01:13:36Z</dc:date>
    <item>
      <title>SAS IML: Simulating continuous and binary variable with a time step index using proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528155#M4510</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to simulate to simulate a longitudinal&amp;nbsp;design in SAS IML for continuous and binary outcomes. Could anyone help with&lt;/P&gt;&lt;P&gt;1) &lt;STRONG&gt;Providing a more efficient and shorter way to write the following continuous simulation in SAS/IML&lt;/STRONG&gt;.&amp;nbsp;I tried to find an alternative way (less coding) to do this loop using RANDGEN or RAND for both continuous and binary variable. On page 227 of Simulating data with sas&amp;nbsp;(Rick Wiclkin), there is an example but I couldn't adapt it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
		call randseed(1);
		time = T( do(1, 10, 1));
		std=1;
		N =5;
		grandmean=20;
		whz = j(nrow(time), N,.);
		whz[1,]=0.2; /*initialize the first instance*/

		ei = j(1,N);
		do i = 2 to nrow(time);
			call randgen(ei, "Normal", 0, std);
			whz[i,] =  grandmean + whz[i-1,] + ei;
		end;
		print time whz;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;2) Providing an example for the binary one. I couldn't get my code to work&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
		call randseed(1);
		time = T( do(1, 10, 1));
		N =5;
		pa = j(nrow(time), N,.);
		pa[1,]=0; /*initialize the first instance*/

		do i = 2 to nrow(time);
			call randgen(pa, "bernouilli", logistic( 2*[i-1,]));
		end;
		print time pa;
/*This did not work*/&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;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jan 2019 20:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528155#M4510</guid>
      <dc:creator>SimRock</dc:creator>
      <dc:date>2019-01-17T20:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: SAS IML: Simulating continuous and binary variable with a time step index using proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528214#M4511</link>
      <description>&lt;P&gt;You haven't described what you want (1) to do.&amp;nbsp;The page you quote shows&amp;nbsp;an ANOVA example, but&amp;nbsp;this code looks like a time series in which each step increments by 20 (plus random amount) from the previous&amp;nbsp;time step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For (2), it looks like you have forgotten 'pa' in the argument to the LOGISTIC function, but again I don't know what you are trying to accomplish.:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;call randgen&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;pa&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"bernouilli"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;logistic&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*pa&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;  /* ??? */&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jan 2019 23:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528214#M4511</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-17T23:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: SAS IML: Simulating continuous and binary variable with a time step index using proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528223#M4512</link>
      <description>&lt;P&gt;Thank you Rick,&lt;/P&gt;&lt;P&gt;I wanted to be able to simulate future whz[t] and pa[t] as a function of past whz[t-1]&amp;nbsp; and pa[t-1], respectively. In other words, I wanted to get data similar to this data step simulation but I'd like to do it in SAS IML.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start=1;
%let endpoint=10;
%let grandmean=20;
%let std=1;
data simregwide;
      array arwhz	{&amp;amp;start:&amp;amp;endpoint} whz&amp;amp;start-whz&amp;amp;endpoint;
	  array arpa	{&amp;amp;start:&amp;amp;endpoint} pa&amp;amp;start-pa&amp;amp;endpoint;
	  
		call streaminit(1234);
		do id=1 to 5;
			do t = &amp;amp;start to &amp;amp;endpoint;
			  if t =1 then
					do;
					   arwhz[1]=0.2;
					   arpa[1]=0;
					end;

				else
					do;
					    arwhz[t]=rand("normal", &amp;amp;grandmean + arwhz[t-1], &amp;amp;std);
			  			arpa[t]=rand("bernouilli", logistic(2*arpa[t-1]));
			        end;
			
			end;
    		output;
		end;
		keep id whz1-whz10 pa1-pa10;
run;

Data simreglong ;
retain id t whz pa;
	set simregwide;
	  array arwhz	{&amp;amp;start:&amp;amp;endpoint} whz&amp;amp;start-whz&amp;amp;endpoint;
	  array arpa	{&amp;amp;start:&amp;amp;endpoint} pa&amp;amp;start-pa&amp;amp;endpoint;

	do t = &amp;amp;start to &amp;amp;endpoint;
			whz =	arwhz[t];
			pa = 	arpa[t];
	output;
	end;

	keep 	id t whz pa;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Thank you&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jan 2019 01:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528223#M4512</guid>
      <dc:creator>SimRock</dc:creator>
      <dc:date>2019-01-18T01:13:36Z</dc:date>
    </item>
    <item>
      <title>Re: SAS IML: Simulating continuous and binary variable with a time step index using proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528297#M4513</link>
      <description>&lt;P&gt;OK. Thanks for the additional details. Your IML program already performs these computations, so my responses are&lt;/P&gt;
&lt;P&gt;&amp;gt; &lt;SPAN&gt;1)&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Providing a more efficient and shorter way to write the following continuous simulation in SAS/IML&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only random variable is the error term, and all errors are independent. You can't avoid the loop b/c it's a time series,&amp;nbsp;but I'd suggest incorporating the grandmean&amp;nbsp;into the call to RANDGEN and using&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
call randseed(1);
time = T( do(1, 10, 1));
std=1;
N =5;
grandmean=20;
whz = j(nrow(time), N,.);
e = whz;
call randgen(e, "Normal", grandmean, std); /* generate all N(mu, sigma) */

whz[1,]=0.2; /*initialize the first instance*/
do i = 2 to nrow(time);
   whz[i,] =  whz[i-1,] + e[i-1,];
end;
print time whz;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;gt; 2) Providing an example for the binary one. I couldn't get my code to work&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your probability depends only on the previous value of arpa, so the call to RANDGEN has to go inside the loop. Otherwise, the same ideas apply:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;arpa = j(nrow(time), N, 0);
a = j(1, N, 0);
do i = 2 to nrow(time);
   call randgen(a, "Bernoulli", logistic(2*arpa[i-1, ]));
   arpa[i,] =  a;
end;
print arpa;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Jan 2019 14:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/528297#M4513</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-18T14:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: SAS IML: Simulating continuous and binary variable with a time step index using proc IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/529227#M4514</link>
      <description>&lt;P&gt;Thank you so much, Rick. This worked. I have a follow-up question on how to summarize the results by time points but I'll ask in a different post for clarity&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jan 2019 19:17:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Simulating-continuous-and-binary-variable-with-a-time/m-p/529227#M4514</guid>
      <dc:creator>SimRock</dc:creator>
      <dc:date>2019-01-22T19:17:51Z</dc:date>
    </item>
  </channel>
</rss>

