BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PaalNavestad
Pyrite | Level 9

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.

 

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.

 

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.

 

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.

 

I do not have IML avalialable

 

%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= &prop01  hrs=&hrs01;
%put pr1= &prop04  hrs=&hrs04;

Data events;
	prob_by_day=30/&avg_mnth;
	do i=0 to 5000 by 1;
		date = intnx('day','01jul2017'd,i);
		p=rand('POISSON',1/prob_by_day);
		if p > 0 then do;
			do j=1 to p by 1;
				draw=rand('UNIFORM');
				if draw =< &prop01 then hrs=&hrs01;
				else if draw =< &prop02 then hrs=&hrs02;
				else if draw =< &prop03 then hrs=&hrs03;
				else if draw =< &prop04 then hrs= &hrs04;
				c=1;
				output;
			end;
		end;
		else do;
			draw =.;
			hrs=.;
			c=0;
			output;
		end;
		
	end;
	drop j;

	format date date9.;
run;

 

1 ACCEPTED SOLUTION
2 REPLIES 2
PaalNavestad
Pyrite | Level 9

Thanks a million works like a dream.

 

Easy to forget these nice functions that have been around since forever,

 

Here is the revised code.

%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/&avg_mnth;
	do i=0 to 5000 by 1;
		date = intnx('day','01jul2017'd,i);
		p=rand('POISSON',1/prob_by_day);
		if p > 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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 635 views
  • 0 likes
  • 2 in conversation