BookmarkSubscribeRSS Feed
SimRock
Obsidian | Level 7

Hi,

I am trying to simulate a longitidunal data with time-varying variables but I am having some trouble. The code seems to be fine but it's not yielding an ouput (i.e. it runs forever). The problem arise when I use the do while statements. Basically when Yt = 1, no records are included for that individual from time t + 1 onwards. For instance, if for individuals with Y1 =1, there should be no record available (L2, A2 and Y2 will be missing).

Can someone help me fix the code. Thank you

 

Data test;
Call streaminit(234);

do id = 1 to 1000;
U = rand("normal",0, 0.25);
L0 = rand("normal", 5.5 + U, 0.04);
A0 = rand("Bernoulli", expit(5 - L0));

L1 = rand("normal",0.9*L0 + A0 + 0.1*U, 0.01);
A1 = rand("bernoulli", expit(A0 + 4.5*L1));
Y1 = rand("bernoulli", expit(-8 + L0 - 0.3*(A0) - U));

 

do while (Y1=0);
L2 = rand("normal",0.9*L1 + A1 + 0.1*U, 0.01);
A2 = rand("bernoulli", expit(A1 + 4.5*L2)) ;
Y2 = rand("bernoulli", expit(-8 + L1 - 0.3*(A0 + A1) - U));
end;

do while (Y2=0);
Y3 = rand("bernoulli", expit(-8 + L2 - 0.3*(A0 + A1 + A2) - U));
end;


output;

end;
run;

4 REPLIES 4
Reeza
Super User
In your do while loops you never change the variable that causes you to enter the loop (Y1, Y2) so you'll never exit the loop if you enter it.

If Y1=0, you enter the loop, never change Y1 and are stuck...

do while (Y1=0);
L2 = rand("normal",0.9*L1 + A1 + 0.1*U, 0.01);
A2 = rand("bernoulli", expit(A1 + 4.5*L2)) ;
Y2 = rand("bernoulli", expit(-8 + L1 - 0.3*(A0 + A1) - U));
end;
SimRock
Obsidian | Level 7

Thank you Reeza, that was really helpful

So do you think I could do something like this. 

Data test;
Call streaminit(234);

do id = 1 to 1000;
U = rand("normal",0, 0.25);
L0 = rand("normal", 5.5 + U, 0.04);
A0 = rand("Bernoulli", expit(5 - L0));

L1 = rand("normal",0.9*L0 + A0 + 0.1*U, 0.01);
A1 = rand("bernoulli", expit(A0 + 4.5*L1));
Y1 = rand("bernoulli", expit(-8 + L0 - 0.3*(A0) - U));

 

if (Y1=0) then do;
L2 = rand("normal",0.9*L1 + A1 + 0.1*U, 0.01);
A2 = rand("bernoulli", expit(A1 + 4.5*L2)) ;
Y2 = rand("bernoulli", expit(-8 + L1 - 0.3*(A0 + A1) - U));
end;

if (Y2=0) then do;
Y3 = rand("bernoulli", expit(-8 + L2 - 0.3*(A0 + A1 + A2) - U));
end;


output;

end;
run;

 

However, it does not seem to quite work because people with Y1=1 still have records for L2, A2, Y2, which does not makes sense.

Can you share your wisdom once more

Thanks

 

Reeza
Super User
You may also notice the values are the same as the previous record. Because there is no data set, there is no output and reinitialization of the PDV.

You need to set them to missing at the top of the loop. I recommend the call missing routine.



SimRock
Obsidian | Level 7

Thank you very much Reeza. It worked out well. Now I am trying to automate the process by using functions like arrays, but I am having a bit of trouble. I want it to go from t=0 to t=9 for variabes A and L. For the Y variables, I would like it to go from t=1 to t=10.Could you provide some guidance.

 

Data sim;
Call streaminit(234);
array A{0:9} A0-A9; array L{0:9} L0-L9 ; array Y{1:10} Y1-Y10;
do id = 1 to 100;

do t=0 to 1;
U = rand("normal",0, 0.25);
L{0} = rand("normal", 5.5 + U, 0.04);
A{0} = rand("Bernoulli", expit(5 - L{0}));

L{1} = rand("normal",0.9*L{0} + A{0} + 0.1*U, 0.01);
A{1} = rand("bernoulli", expit(A{0} + 4.5*L{1}));
Y{1} = rand("bernoulli", expit(-8 + L{0} - 0.3*(A{0}) - U));
end;

do t=2 to 8;
if (Y{t-1}=0) then do;
L{t} = rand("normal",0.9*L{t-1} + A{t-1} + 0.1*U, 0.01);
A{t} = rand("bernoulli", expit(A{t-1} + 4.5*L{t})) ;
Y{t} = rand("bernoulli", expit(-8 + L{t-1} - 0.3*sum(of A{0} to A{t-1}) - U));
end; else do L{t}=.; A{t}=.; Y{t}=.; end;
end;

t=9;
if (Y{t}=0) then do;
Y{t+1} = rand("bernoulli", expit(-8 + L{t} - 0.3*sum(of A{0} to A{t}) - U));
end; else do Y{t+1}=.; end;

output;
end;

run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1297 views
  • 0 likes
  • 2 in conversation