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

I have lots of data as follows:

 

FlightIDEntryExitNMFIR_IDFIR
948328410165351074EDMONTON
94832841165290651074EDMONTON
94832841290330311074EDMONTON
948328413303302561079WINNIPEG
94832841330330901079WINNIPEG
948328413303501671079WINNIPEG
948328413503502411079WINNIPEG
948328413503501981079WINNIPEG
94832841350350141078TORONTO
948328413503302071078TORONTO
94832841330280801078TORONTO
94832841280132481078TORONTO
948328411320261078TORONTO

 

Every flight has a unique FlightID and each flight can pass through regions (FIR). I'd like to be able to create a flight_segment variable.

 

Every time a NEW FlightID is found, that would be the start of a flight and thus the start of a segment...that is flight_segment would equal 1. If the next FlightID is the same AND the FIR is the same, it would remain at 1. If the next FlightID is the SAME and the FIR has changed, then the flight_segment would have one added to it.

 

Any ideas?

 

Thx

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Post example data in the form of a data step.

 

Not tested as no data provided:

data want;
   set have;
   by notsorted flightid notsorted FIR;
   if first.flightid then segment=0;
   if first.fir then segment+1;
run;

View solution in original post

2 REPLIES 2
ballardw
Super User

Post example data in the form of a data step.

 

Not tested as no data provided:

data want;
   set have;
   by notsorted flightid notsorted FIR;
   if first.flightid then segment=0;
   if first.fir then segment+1;
run;
AMSAS
SAS Super FREQ

You can use SQL to do it

data have ;
	input flightID $ entry $ exit $ nm $ fir_id $ fir $ ;
cards ;
94832841	0	165	35	1074	EDMONTON
94832841	165	290	65	1074	EDMONTON
94832841	290	330	31	1074	EDMONTON
94832841	330	330	256	1079	WINNIPEG
94832841	330	330	90	1079	WINNIPEG
94832841	330	350	167	1079	WINNIPEG
94832841	350	350	241	1079	WINNIPEG
94832841	350	350	198	1079	WINNIPEG
94832841	350	350	14	1078	TORONTO
94832841	350	330	207	1078	TORONTO
94832841	330	280	80	1078	TORONTO
94832841	280	132	48	1078	TORONTO
94832841	132	0	26	1078	TORONTO
94832842	0	165	35	1074	EDMONTON
94832842	165	290	65	1074	EDMONTON
94832842	290	330	31	1074	EDMONTON
94832842	330	330	256	1079	WINNIPEG
94832842	330	330	90	1079	WINNIPEG
94832842	330	350	167	1079	WINNIPEG
94832842	350	350	241	1079	WINNIPEG
94832842	350	350	198	1079	WINNIPEG
;

proc sql ;
	select flightid, count(unique(fir))
	from have
	group by flightid 
	
	;
quit ;

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
  • 2 replies
  • 553 views
  • 1 like
  • 3 in conversation