BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
duft
Calcite | Level 5

Hi,

 

I'm trying to write codes for a loop to simulate Arrivals and Departures. There's only one server (M/M/1) and it is a FCFS. So if someone arrives when the server is busy, then they have to wait for the server to be free.

data have;
	input id time_of_arrival service_time;
	datalines;
	  1 1.5 2
	  2 2 1  
	  3 3.5 2  
	  4 8 1  
	  5 10 2  
	  6 12 1  
	  7 13 2  
	  8 14 2  
	  9 16 3  
 run;
 
 data have;
	set have;
	time_of_leaving = time_of_arrival + service_time;
	lag_time_of_leaving = lag(time_of_leaving);
	if lag_time_of_leaving > time_of_arrival then flag = 1; else flag = 0;
run;

I thought about creating another variable = 0 (flag2) and loop until flag = flag2 (see following code - it added it before the run; command in the previous code). However, the program has been running for the past hour and my initial thought is that there must be a more efficient way of doing it.

	flag2 = 0;
	do until (flag = flag2);
		if flag = 1 then time_of_leaving = lag_time_of_leaving + service_time;
		lag_time_of_leaving = lag(time_of_leaving);
		if lag_time_of_leaving > time_of_arrival then flag = 1; else flag = 0;
	end;

 

In the end, I'd like for the results to be as followed

 

idtime_of_arrivalservice_timetime_of_leaving
11.523.5
2214.5 (server is free at 3.5 + service_time of 1)
33.526.5
4819
510212
612113
713215
814217
916320

 

Thank you in advance for your help

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
 input id time_of_arrival service_time;
 retain want;
    want=ifn(time_of_arrival<want,want+service_time,time_of_arrival+service_time);
 datalines;
   1 1.5 2
   2 2 1  
   3 3.5 2  
   4 8 1  
   5 10 2  
   6 12 1  
   7 13 2  
   8 14 2  
   9 16 3  
 run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

I don't fully understand this.

 

Can you post what your desired result looks like?

duft
Calcite | Level 5
I've updated the post. Sorry if it wasn't clear
Ksharp
Super User
data have;
 input id time_of_arrival service_time;
 retain want;
    want=ifn(time_of_arrival<want,want+service_time,time_of_arrival+service_time);
 datalines;
   1 1.5 2
   2 2 1  
   3 3.5 2  
   4 8 1  
   5 10 2  
   6 12 1  
   7 13 2  
   8 14 2  
   9 16 3  
 run;
duft
Calcite | Level 5
Thank you! I never used ifn, definitely something to remember.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1429 views
  • 0 likes
  • 3 in conversation