Skip to content

Instantly share code, notes, and snippets.

View MJacobs1985's full-sized avatar

Marc Jacobs MJacobs1985

View GitHub Profile
/* Significant effects where there are none */
%let ErrorVariance=1000;
%let nrep=10;
%let nsim=100;
DATA CRD;
call streaminit(123);
do isim = 1 to ≁
do rep=1 to &nrep;
do trt=1 to 5;
if trt=1 then y=791.5 + rand('Normal', 0, sqrt(&ErrorVariance));
@MJacobs1985
MJacobs1985 / Normality of errors
Created September 29, 2021 18:54
Simulations in SAS
data REGRESS2;
call streaminit(12345);
do i=1 to 1000 by 1;
x=rand("Uniform",0,100);
error=rand("Normal", 0, 0.2);
y=2+(0.5*x)+error;
output;
end;
run;
proc univariate data=REGRESS2;
@MJacobs1985
MJacobs1985 / Simulate Poisson Data
Created September 29, 2021 18:58
Simulations in SAS
/* Tabulate counts and plot data */
%let N=150;
data CountData;
array xx1{&N} _temporary_;
array xx2{&N} _temporary_;
call streaminit(1);
/* simulate fixed effects */
do i=1 to &N;
xx1{i}=rand("Uniform");
xx2{i}=rand("Normal", 0 , 0.5);
/** Simulate binomial data, then analyze it it with Logistic Regression **/
/* Simulate dataset */
%let N=150;
data LogisticData;
array xx1{&N} _temporary_;
array xx2{&N} _temporary_;
call streaminit(1);
/* simulate fixed effects */
do i=1 to &N;
xx1{i}=rand("Uniform");
@MJacobs1985
MJacobs1985 / IML
Created September 29, 2021 19:11
Simulations in SAS
/* PROC IML --> using different degrees of freedom */
ods graphics on / outputfmt=svg;
proc iml;
call randseed(1234); /* set random number seed */
n = j(100,1);
t2 = j(100,1);
t4 = j(100,1);
t8 = j(100,1);
t16 = j(100,1);
t32 = j(100,1);
@MJacobs1985
MJacobs1985 / Nested Design
Created September 29, 2021 19:23
Simulations in SAS
%let CovBlock=0.000819;
%let CovPen=0.001557;
%let CovResidual=0.03129;
data sim;
array trtmean{4} _temporary_ (2.1223 2.1046 2.0919 2.0773);
array covpen{4} _temporary_ (0.001361 0.000012 0.002008 0.002880);
do blocknum=12;
do animalnum=2 to 25 by 2;
do sim=1 to 1000 by 1;
do block=1 to blocknum;
@MJacobs1985
MJacobs1985 / Gompertz
Created September 29, 2021 19:29
Simulations in SAS
data IB;
do sim=1;
do dose=0,20,40,60,80,100,160 ;
do pen=1 to 100;
if dose=0 then Y=200 + rand("Normal", 0, sqrt(480.779));
if dose=20 then Y=304 + rand("Normal", 0, sqrt(480.779));
if dose=40 then Y=317 + rand("Normal", 0, sqrt(480.779));
if dose=60 then Y=321 + rand("Normal", 0, sqrt(480.779));
if dose=80 then Y=326 + rand("Normal", 0, sqrt(480.779));
if dose=100 then Y=332 + rand("Normal", 0, sqrt(480.779));
@MJacobs1985
MJacobs1985 / Gompertz - 2
Created September 29, 2021 19:36
Simulations in SAS
%let nsim=100;
data power;
length TT $30;
array a[4] _temporary_ (314.31 347 314.31 308.4);
array k[4] _temporary_ (0.06158 0.00975 0.06158 0.0296);
array t[4] _temporary_ (-15.442 -205.4 -18.9413 -63.43); /* You can already see that the estimates of T are not precies and helpfull --> carefull attention is needed for these */
do sim=1 to ≁
do treatment=1 to 4;
if treatment=1 then TT="A";
if treatment=2 then TT="B";
@MJacobs1985
MJacobs1985 / IML - Multivariate Normal
Last active September 29, 2021 19:54
Simulations in SAS
%let SigmaBlock=2520.38;
%let block=11;
%let treat=9;
%let time=2;
%let isim=200;
proc iml;
/* Create dataset */
create WIDEAMEN var {"ISIM" "Block" "RndBlock" "Time" "T1" "T2" "T3" "T4" "T5" "T6" "T7" "T8" "T9"};
call randseed(4321);
/* Create # simulations of correlated data for 9 Treatments & Two Timepoints */
@MJacobs1985
MJacobs1985 / Importing and changing data
Last active September 30, 2021 08:13
Time-series analysis in SAS
FILENAME REFFILE '//nlws0844/mjacobs$/StatisticsPlatform/Website/ELearning/Workshops/Time-Series/Data/Deceased.xlsx';
PROC IMPORT DATAFILE=REFFILE
DBMS=XLSX
OUT=WORK.IMPORT;
GETNAMES=YES;
RUN;
/* Delete row
/** Content of the data **/
proc freq data=import nlevels;table year*week;run;
/** DATA WRANGLING **/