## Code to perform simulation

Solved
Frequent Contributor
Posts: 109

# Code to perform simulation

I've a data set of 70 cases, and I have an equation involving 17 variables from the data that I'd like to test on. Specifically I'd like to test if

1) changing +/-5% magnitude of each variable ^one at a time^, what would the variation for the Y value on the left of the equation and changing which variable would produce the largest variation.

2) changing +/-5% magnitude of each variable ^one at a time^, summing up the Y value and calculating the proportion of each case (so the sum if 100%), what is the variation for share % and changing which variable would produce the largest variation in the share?

3) The same as #2, except changing +/-5% magnitude of each variable ^one at a time^ and one case a time, which variable would produce the the largest variation for the overall data and which case is most impacted by this change. (Hope this question makes sense).

I got some great tips from and at the Programming forum  https://communities.sas.com/t5/Base-SAS-Programming/Code-to-perform-simulation/m-p/279203#U279203 and were suggested to post here. Ksharp gave me a code for #1 but it has a bit of error. How do I fix?

And how do I approach #2?

Doing simulation in SAS is all new to me and it's a bit urgent for work. (I'll definetely need to read more on the subject for future work).

``````data have; input
ID A B C D E N1 N2 N3 N4 N5 N6 D1 D2 D3 D4 D5 D6;
datalines;
1  100 10 0.5 15 10  35 8 6 4 2 1 39 8 5 5 1 2
2  200 11 0.3 25 15  35 8 6 4 2 2 42 7 7 4 2 3
3  300 20 0.4 15 14  40 8 6 4 2 3 53 9 6 5 3 2
4  150 19 0.1  5 19  27 8 6 4 2 4 35 4 9 7 4 4
5  400 40 0.5 35 20 50 8 6 4 2 6 79 17 4 9 1 5
6  900 50 0.4 55 15  70 8 6 4 2 9 85 5 9 4 2 11
7  600 30 -0.5 20 29  60 8 6 4 2 2 62 16 7 5 4 10
8  500 30 0.2 25 17  50 8 6 4 2 3 60 4 9 8 2 5
9  400 30 0.4 19 16  44 8 6 4 2 1 49 8 4 4 5 9
10  700 45 0.3 39 14  34 8 6 4 2 1 38 3 2 5 4 12
;
run;

proc iml;
use have(keep= A B C D E);
read all var _num_ into have[c=vnames];
close;

use have(keep= N1-N6);
read all var _num_ into N;
close;

use have(keep= D1-D6);
read all var _num_ into D;
close;

Parm_N={1 -1 -1 -1 -1 -1};
Parm_D={1 -1 -1 -1 -1 -1};
magnitude={-1.05 1.05};
Y=j(nrow(have),1,.);
id=j(ncol(magnitude)#ncol(have),1,blankstr(40));
mean_std=j(ncol(magnitude)#ncol(have),2,.);
mattrib mean_std c={mean std};

idx=0;
do i=1 to ncol(magnitude);
do j=1 to ncol(vnames);
Parm_have=j(1,ncol(have),1);
Parm_have[j]=magnitude[i];
do k=1 to nrow(have);
temp=have[k,];
wgt=(N[k,]*Parm_N`)/(D[k,]*Parm_D`);
wgt=choose(wgt>1,1,wgt);
Y[k]=(Parm_have[1]#temp[1]#
Parm_have[2]#temp[2]#
(Parm_have[3]#temp[3]+1)
-Parm_have[4]#temp[4]
-Parm_have[5]#temp[5])#wgt;
end;
idx=idx+1;
id[idx]=char(magnitude[i])+'*'+vnames[j];
mean_std[idx,1]=mean(Y);
mean_std[idx,2]=std(Y);
end;
end;

print id mean_std[l=''];
quit;``````

Accepted Solutions
Solution
‎06-23-2016 07:25 PM
SAS Employee
Posts: 9

## Re: Code to perform simulation

If you are looking to vary the magnitude of each variable by +/- 5%, then you need to change

``magnitude = {-1.05 1.05};``

to

``magnitude = {0.95 1.05};``

If I understand correctly what you are trying to do with 2), you should be able to add

``Y = Y/sum(Y);``

immediately following your loop over k.  Leaving everything else the same *should* give you the answer for 2).  If I have misunderstood what you are trying to accomplish, please elaborate further.

All Replies
Solution
‎06-23-2016 07:25 PM
SAS Employee
Posts: 9

## Re: Code to perform simulation

If you are looking to vary the magnitude of each variable by +/- 5%, then you need to change

``magnitude = {-1.05 1.05};``

to

``magnitude = {0.95 1.05};``

If I understand correctly what you are trying to do with 2), you should be able to add

``Y = Y/sum(Y);``

immediately following your loop over k.  Leaving everything else the same *should* give you the answer for 2).  If I have misunderstood what you are trying to accomplish, please elaborate further.

Frequent Contributor
Posts: 109

## Re: Code to perform simulation

Thanks a lot. Will fix and apply.

🔒 This topic is solved and locked.