<?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: Variable average with missing value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346417#M79892</link>
    <description>&lt;P&gt;Hi art297, thank you for your contribution. I got exactly 3500 returns from state 1 and rest from state 2.&lt;/P&gt;&lt;P&gt;I still have two quesitons with your code.&lt;/P&gt;&lt;P&gt;1. Is there any possible reason why my code generate 3400 R1 and 1600 R2?&lt;/P&gt;&lt;P&gt;2. After grouping and creating the returns, how to get the mean, variance, standard deviation, densitiy, etc for return from state1 and from state 2 separately?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 01 Apr 2017 17:33:23 GMT</pubDate>
    <dc:creator>Xusheng</dc:creator>
    <dc:date>2017-04-01T17:33:23Z</dc:date>
    <item>
      <title>Variable average with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346392#M79877</link>
      <description>&lt;P&gt;Hi, I used simulation to generate mixed distribution returns from state 1 &amp;amp; 2, where N =5000, P(state 1) = 70%, P(state 2) = 30%. Then I separate return 1 and return 2, and filling the missing value as 0. Now the question is how to create the average value scalar for each return? I mean create this number for future use. I tried transpose since the average number does not have the same dimension with my variables. However, after I count for the number of each return, R1 has 3400 whil R2 has 1600. Should these be 3500 for R1 and 1500 for R2? I would appreciate any comments and suggestion. Following&amp;nbsp;is my sample and code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* sample from a mixture distribution */
%let N = 5000;
data States(drop=i);
call streaminit(12345);
array prob [2] _temporary_ (0.7 0.3);
do i = 1 to &amp;amp;N;
   type = rand("Table", of prob[*]);
   if type=1 then      Return = rand("Normal", 0, 3); /* state 1 */
   else                Return = rand("Normal", 5, 10); /* steta 2 */             
   output;
end;
run;
data work.States;
	set work.States;
if type = 1 then R1 = Return + 0;
else R2 = Return + 0;
if R1 =. then R1 =0;
if R2 =. then R2 =0;
Run;
proc transpose 
data=work.States;
run;
data want;
set data1;
array cnt col1-col5000;
array cnts cols1-cols5000;
do over cnt;
if cnt in (0,.) then cnts=1;
else cnts=.;
allcnts=nmiss(of cols1-cols5000);
end;
drop col: cols:;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is a short example for the result:&lt;/P&gt;&lt;P&gt;N &amp;nbsp; &amp;nbsp; type &amp;nbsp; &amp;nbsp; return &amp;nbsp; &amp;nbsp; &amp;nbsp;R1 &amp;nbsp; &amp;nbsp; &amp;nbsp; R2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.58 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.58 &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.36 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.36&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; -0.74 &amp;nbsp; &amp;nbsp; &amp;nbsp;-0.74 &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 12:40:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346392#M79877</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2017-04-01T12:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: Variable average with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346405#M79886</link>
      <description>&lt;P&gt;If you want exact proportional assignment you might be better off using proc surveyselect. There is probably a more direct route to get what you want, but the following will work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let N = 5000;
data States;
  do i = 1 to &amp;amp;N;
   output;
  end;
run;

proc surveyselect data=states groups=10 seed=12345 out=states noprint;
run;

data States(drop=i);
  set States;
   if groupid lt 8 then type=1;
   else type=2;
   if type=1 then Return = rand("Normal", 0,  3);/* state 1 */
   else           Return = rand("Normal", 5, 10);/* state 2 */             
   output;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 16:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346405#M79886</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-01T16:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Variable average with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346417#M79892</link>
      <description>&lt;P&gt;Hi art297, thank you for your contribution. I got exactly 3500 returns from state 1 and rest from state 2.&lt;/P&gt;&lt;P&gt;I still have two quesitons with your code.&lt;/P&gt;&lt;P&gt;1. Is there any possible reason why my code generate 3400 R1 and 1600 R2?&lt;/P&gt;&lt;P&gt;2. After grouping and creating the returns, how to get the mean, variance, standard deviation, densitiy, etc for return from state1 and from state 2 separately?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 17:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346417#M79892</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2017-04-01T17:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Variable average with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346425#M79895</link>
      <description>&lt;P&gt;I am definitely NOT an expert regarding the fairly new RAND function. However, that said, based on the UNIFORM distribution and the seed you used, 3416 of the observations you created had a random number generated that was less than .7.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is apparently what the table option is selecting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your multiple calls to the function also likely interferred with a standard expectation. Try the following and look at the output. I use proc means to get the group counts but, at the same time, it (I think) does the calculations you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let N = 5000;
data States(drop=i);
  call streaminit(12345);
  array prob [2] _temporary_ (0.7 0.3);
  do i = 1 to &amp;amp;N;
    type = rand("Table", of prob[*]);
    output;
  end;
run;
data States;
  set States;
  call streaminit(12345);
  if type=1 then      Return = rand("Normal", 0, 3); /* state 1 */
  else                Return = rand("Normal", 5, 10); /* steta 2 */             
run;
data states;
  set states;
  call streaminit(12345);
  rnum = rand("Uniform");
run;
proc sort data=states;
  by rnum;
run;

proc means data=states;
  var return;
  class type;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 19:17:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346425#M79895</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-01T19:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: Variable average with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346430#M79898</link>
      <description>&lt;P&gt;Thank you art297, I've tried your code and it works.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since I would like to use the exact 70% and 30%, I will apply your first method and then use excel to get the parameters I need.&lt;/P&gt;&lt;P&gt;I will consider the second method when I come back to SAS and continue with following steps. Thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 20:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346430#M79898</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2017-04-01T20:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Variable average with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346446#M79902</link>
      <description>&lt;P&gt;Hi Xusheng,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know if this approach is too crude, but why don't you just generate the 5000 i's, and if i is less than or equal to 3500 then type =1 and else type=2, and then you do all the subsequent simulation steps like art297 showed?&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 23:08:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/346446#M79902</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-04-01T23:08:13Z</dc:date>
    </item>
    <item>
      <title>Re: Variable average with missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/347105#M80131</link>
      <description>&lt;P&gt;Thank you ilikesas, I did what you suggest -- create two loop one is from 1 to 3500 and another is from 3501 to 5000. And it works. Thank you so much.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 16:22:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-average-with-missing-value/m-p/347105#M80131</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2017-04-04T16:22:50Z</dc:date>
    </item>
  </channel>
</rss>

