<?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 Looking up a value that is between to other values. Used in simualtion in a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looking-up-a-value-that-is-between-to-other-values-Used-in/m-p/369434#M88197</link>
    <description>&lt;P&gt;Hi I'm trying to simulate descrete events forward in time and the simulate a different value for each of these events. In real life this is to create a schedule and take in consideration events that have not been detected yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The inline code works but is a bit hard to make completly robust. E.g. i have hardcoded 4 groups of values, but in real life it might be more or less. This can be done in a macro but might be hard to debug.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another solution is to join in the from to values for the univarite drawing, test if it is between and then dropping the lines where it is not between. Howvwer this will expand the data and can lead to other errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anybody have a more elegent method. I was thinking about reading the proportion, hrs_darw into an array but do not get it to run.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not have IML avalialable&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let avg_mnth=10;
data WORK.CUM_PROB;
  infile datalines dsd truncover;
  input Proportion:BEST10.9 hrs_draw:32. cum:32.;
datalines4;
0.51818182,6,0.5181818182
0.05909091,7,0.5772727273
0.21363636,10,0.7909090909
0.20909091,18,1
;;;;
run;

data cum_prob;
	set cum_prob;
	
	prop_name='prop'||put(_n_,z2.);
	hrs_name='hrs'||put(_n_,z2.);

	call symput(prop_name,cum);
	call symput(hrs_name,hrs_draw);


	keep proportion cum hrs_draw ;
run;


%put pr1= &amp;amp;prop01  hrs=&amp;amp;hrs01;
%put pr1= &amp;amp;prop04  hrs=&amp;amp;hrs04;

Data events;
	prob_by_day=30/&amp;amp;avg_mnth;
	do i=0 to 5000 by 1;
		date = intnx('day','01jul2017'd,i);
		p=rand('POISSON',1/prob_by_day);
		if p &amp;gt; 0 then do;
			do j=1 to p by 1;
				draw=rand('UNIFORM');
				if draw =&amp;lt; &amp;amp;prop01 then hrs=&amp;amp;hrs01;
				else if draw =&amp;lt; &amp;amp;prop02 then hrs=&amp;amp;hrs02;
				else if draw =&amp;lt; &amp;amp;prop03 then hrs=&amp;amp;hrs03;
				else if draw =&amp;lt; &amp;amp;prop04 then hrs= &amp;amp;hrs04;
				c=1;
				output;
			end;
		end;
		else do;
			draw =.;
			hrs=.;
			c=0;
			output;
		end;
		
	end;
	drop j;

	format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 22 Jun 2017 11:24:54 GMT</pubDate>
    <dc:creator>PaalNavestad</dc:creator>
    <dc:date>2017-06-22T11:24:54Z</dc:date>
    <item>
      <title>Looking up a value that is between to other values. Used in simualtion in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-up-a-value-that-is-between-to-other-values-Used-in/m-p/369434#M88197</link>
      <description>&lt;P&gt;Hi I'm trying to simulate descrete events forward in time and the simulate a different value for each of these events. In real life this is to create a schedule and take in consideration events that have not been detected yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The inline code works but is a bit hard to make completly robust. E.g. i have hardcoded 4 groups of values, but in real life it might be more or less. This can be done in a macro but might be hard to debug.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another solution is to join in the from to values for the univarite drawing, test if it is between and then dropping the lines where it is not between. Howvwer this will expand the data and can lead to other errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anybody have a more elegent method. I was thinking about reading the proportion, hrs_darw into an array but do not get it to run.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not have IML avalialable&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let avg_mnth=10;
data WORK.CUM_PROB;
  infile datalines dsd truncover;
  input Proportion:BEST10.9 hrs_draw:32. cum:32.;
datalines4;
0.51818182,6,0.5181818182
0.05909091,7,0.5772727273
0.21363636,10,0.7909090909
0.20909091,18,1
;;;;
run;

data cum_prob;
	set cum_prob;
	
	prop_name='prop'||put(_n_,z2.);
	hrs_name='hrs'||put(_n_,z2.);

	call symput(prop_name,cum);
	call symput(hrs_name,hrs_draw);


	keep proportion cum hrs_draw ;
run;


%put pr1= &amp;amp;prop01  hrs=&amp;amp;hrs01;
%put pr1= &amp;amp;prop04  hrs=&amp;amp;hrs04;

Data events;
	prob_by_day=30/&amp;amp;avg_mnth;
	do i=0 to 5000 by 1;
		date = intnx('day','01jul2017'd,i);
		p=rand('POISSON',1/prob_by_day);
		if p &amp;gt; 0 then do;
			do j=1 to p by 1;
				draw=rand('UNIFORM');
				if draw =&amp;lt; &amp;amp;prop01 then hrs=&amp;amp;hrs01;
				else if draw =&amp;lt; &amp;amp;prop02 then hrs=&amp;amp;hrs02;
				else if draw =&amp;lt; &amp;amp;prop03 then hrs=&amp;amp;hrs03;
				else if draw =&amp;lt; &amp;amp;prop04 then hrs= &amp;amp;hrs04;
				c=1;
				output;
			end;
		end;
		else do;
			draw =.;
			hrs=.;
			c=0;
			output;
		end;
		
	end;
	drop j;

	format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2017 11:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-up-a-value-that-is-between-to-other-values-Used-in/m-p/369434#M88197</guid>
      <dc:creator>PaalNavestad</dc:creator>
      <dc:date>2017-06-22T11:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Looking up a value that is between to other values. Used in simualtion in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-up-a-value-that-is-between-to-other-values-Used-in/m-p/369436#M88198</link>
      <description>&lt;P&gt;How about creating value formats with ranges and apply those to determine groups?&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2017 11:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-up-a-value-that-is-between-to-other-values-Used-in/m-p/369436#M88198</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-22T11:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: Looking up a value that is between to other values. Used in simualtion in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-up-a-value-that-is-between-to-other-values-Used-in/m-p/369461#M88207</link>
      <description>&lt;P&gt;Thanks a million works like a dream.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Easy to forget these nice functions that have been around since forever,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the revised code.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let avg_mnth=15;
data WORK.CUM_PROB;
  infile datalines dsd truncover;
  input Proportion:BEST10.9 hrs_draw:32. cum:32.;
datalines4;
0.51818182,6,0.5181818182
0.05909091,7,0.5772727273
0.21363636,10,0.7909090909
0.20909091,18,1
;;;;
run;

data to_fmt;
	set cum_prob;
	retain start end;
	prev=lag(proportion);
	if _n_=1 then do;
		start=0;
		end=proportion;
		SEXCL='N';
	end;
	else do;
		start=end;
		end=start+proportion;
		sexcl='Y';
	end;
	label=put(hrs_draw,best.);
	type='I';
	fmtname='hrsval';
run;

proc format cntlin=to_fmt;
run;


Data events;
	prob_by_day=30/&amp;amp;avg_mnth;
	do i=0 to 5000 by 1;
		date = intnx('day','01jul2017'd,i);
		p=rand('POISSON',1/prob_by_day);
		if p &amp;gt; 0 then do;
			do j=1 to p by 1;
				draw=rand('UNIFORM');
				hrs=input(draw,hrsval.);
				c=1;
				output;
			end;
		end;
		else do;
			draw =.;
			hrs=.;
			c=0;
			output;
		end;
		
	end;
	drop j;

	format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Jun 2017 13:05:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-up-a-value-that-is-between-to-other-values-Used-in/m-p/369461#M88207</guid>
      <dc:creator>PaalNavestad</dc:creator>
      <dc:date>2017-06-22T13:05:45Z</dc:date>
    </item>
  </channel>
</rss>

