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

I have the following data

 

 

PS      Lat            Long         SLOT

abc    75.8233    26.2454     1
abc    75.4785    26.4589     1

abc    75.5495    26.4679     1
abc    75.4875    26.8997     2
abc    75.5649    26.7268     2
abc    75.5666    26.8647     2
xyz    75.4875    26.3842      1
xyz    75.4937    26.5877      1
xyz    75.8473    26.3971      1

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

 

 

Now I want a new column which shows the distances in the following way:

All the points with PS=abc and Slot=1  should have their distances calculated from the first point of this group(i.e.,  75.8233    26.2454), similarly all the points with PS=abc and Slot=2 should have their distances calculated from the first point of this group(i.e., 75.4875    26.8997) and so on.

 

So please suggest me a solution for this.

Also the points should not be entered manually, a programmable solution is needed.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep, we are not here to type test data in nor guess what the structure is.  Hence this code is just a guess, also the output is a guess as you have not shown what you want out.

data first;
  set have;
  by ps slot;
  if first.slot then output;
run;
data want;
  merge have first (rename=(lat=comp_lat long=comp_long));
  by ps slot;
  dist=lat - comp_lat;
  dist2=long - comp_long;
run;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep, we are not here to type test data in nor guess what the structure is.  Hence this code is just a guess, also the output is a guess as you have not shown what you want out.

data first;
  set have;
  by ps slot;
  if first.slot then output;
run;
data want;
  merge have first (rename=(lat=comp_lat long=comp_long));
  by ps slot;
  dist=lat - comp_lat;
  dist2=long - comp_long;
run;
Himanshu007
Obsidian | Level 7
Thnx a lot, it's exactly what I wanted.
Ksharp
Super User

You owe me one hundred dollars .

 

data have;
input PS   $   Lat            Long         SLOT;
cards;
abc    75.8233    26.2454     1
abc    75.4785    26.4589     1
abc    75.5495    26.4679     1
abc    75.4875    26.8997     2
abc    75.5649    26.7268     2
abc    75.5666    26.8647     2
xyz    75.4875    26.3842      1
xyz    75.4937    26.5877      1
xyz    75.8473    26.3971      1
;
run;

data first rest;
 set have;
 by ps slot;
 if first.slot then output first;
  else output rest;
run;
proc sql;
select b.*,1000*geodist(a.lat,a.long,b.lat,b.long,'k') as distance_in_meters
 from first as a,rest as b 
  where a.ps=b.ps and a.slot=b.slot ;
quit;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 764 views
  • 2 likes
  • 3 in conversation