BookmarkSubscribeRSS Feed
Grandhi4
Calcite | Level 5

Hi,

I have a data set in that three variables like 

Boarding point      Destination point      Distance_KM

Delhi                    Mumbai                    2000

Mumbai               Delhi                         2000

Delhi                    Kolkata                    4000

Kolkata                    Delhi                    4000

Mumbai               Kolkata                    6000

Kolkata               Mumbai                    6000

.........                 .........                     .........

.........                 .........                     .........

.........                 .........                     .........

.........                 .........                     .........

How to find a 'First occurrence' ?

How to do 'First. & Last.'

can any one please help me Smiley Sad

Thanks,

Suresh

5 REPLIES 5
art297
Opal | Level 21

In your example, which records are you trying to identify and based on which factors?

Grandhi4
Calcite | Level 5

Hi Arthur,

Boarding point      or     Destination point

Thanks,

Suresh

ballardw
Super User

I'm guessing that you're looking to get one of each boarding destination pair. So something like this maybe:

data intermediate;

     length link $ 200.; /* long enough to take both boarding and destination together*/

     if upcase(boarding) ge upcase(destination) then link = catx(' ',boarding, destination);

     else link = catx(' ', destination,boarding);

run;

proc sort data=intermediate; by link; run;

data want;

     set intermediate;

     by link;

     if first.link;

run;

art297
Opal | Level 21

I'm also just guessing what you want.  If you want to know which record a city was identified as being either a boarding or destination point, you could do it using the in function with arrays.  E.g.:

data have;

  input Boarding_point $ Destination_point $ Distance_KM;

  cards;

Delhi                Mumbai                    2000

Mumbai               Delhi                     2000

Delhi                Kolkata                   4000

Kolkata              Delhi                     4000

Mumbai               Kolkata                   6000

Kolkata              Mumbai                    6000

;

data want (keep=City Record_Number Type);

  array cities(100) $;

  retain cities;

  set have;

  if not(Boarding_point in cities) then do;

    j+1;

    cities(j)=Boarding_point;

    Record_Number=_n_;

    City=Boarding_point;

    Type="BP";

    output;

  end;

  if not(Destination_point in cities) then do;

    j+1;

    cities(j)=Destination_point;

    Record_Number=_n_;

    City=Destination_point;

    Type="DP";

    output;

  end;

run;

proc print data=want;

run;

output

Obs  Record_Number  City     Type

1        1       Delhi       BP

2        1       Mumbai      DP

3        3       Kolkata     DP

shivas
Pyrite | Level 9

Hi,

Try this...is this what you want(Guess)

data have1;

  input Boarding_point $ Destination_point $ Distance_KM;

  newvar=catx('',Boarding_point,Destination_point);

  newvar1=catx('',Destination_point,Boarding_point);

  xx=lag(newvar1);

  if xx=newvar then output;

  cards;

Delhi                Mumbai                    2000

Mumbai               Delhi                     2000

Delhi                Kolkata                   4000

Kolkata              Delhi                     4000

Mumbai               Kolkata                   6000

Kolkata              Mumbai                    6000

;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 959 views
  • 6 likes
  • 4 in conversation