<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to deal with missing coordinate data when computing geodistance by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956203#M373405</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264750"&gt;@RandoDando&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you're almost there. Just store the value returned by the GEODIST function in variable &lt;FONT face="courier new,courier"&gt;dist_prev&lt;/FONT&gt; rather than computing&amp;nbsp;&lt;FONT face="courier new,courier"&gt;dist_prev&lt;/FONT&gt;&amp;nbsp;from cumulative distances. You can also omit the second DATA step (which is missing a BY statement!) and do all calculations in the first as shown below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
call streaminit(27182818);
do g=1 to 5;
  date=rand('integer','01JAN2022'd,'31DEC2022'd);
  do location=1 to 30;
    group=byte(64+g);
    date+rand('integer',50);
    if rand('bern',0.7) then do;
      lat=rand('uniform',30,60);
      long=rand('uniform',-165,-95);
    end;
    else call missing(lat, long);
    output;
  end;
end;
format date yymmdd10.;
run;

/* Compute distances, cumulative distances and time differences */

data want;
set have;
by group;
dist_prev=geodist(lat, long, lag(lat), lag(long));
cum_dist+dist_prev;
days_prev=dif(date);
if first.group then do;
  cum_dist = 0;
  call missing(dist_prev, days_prev);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Jan 2025 15:47:35 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2025-01-15T15:47:35Z</dc:date>
    <item>
      <title>How to deal with missing coordinate data when computing geodistance by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956189#M373403</link>
      <description>&lt;P&gt;I have data which has groups and geographic coordinates.&amp;nbsp; It looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="322"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;Group&lt;/TD&gt;
&lt;TD width="64"&gt;Location&lt;/TD&gt;
&lt;TD width="66"&gt;Date&lt;/TD&gt;
&lt;TD width="64"&gt;Lat&lt;/TD&gt;
&lt;TD width="64"&gt;Long&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;13&lt;/TD&gt;
&lt;TD&gt;1/4/2024&lt;/TD&gt;
&lt;TD&gt;32.58497&lt;/TD&gt;
&lt;TD&gt;-95.5545&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;16&lt;/TD&gt;
&lt;TD&gt;4/2/2024&lt;/TD&gt;
&lt;TD&gt;34.07736&lt;/TD&gt;
&lt;TD&gt;-99.9309&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;25&lt;/TD&gt;
&lt;TD&gt;1/15/2023&lt;/TD&gt;
&lt;TD&gt;35.6381&lt;/TD&gt;
&lt;TD&gt;-104.508&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;256&lt;/TD&gt;
&lt;TD&gt;8/1/2024&lt;/TD&gt;
&lt;TD&gt;37.27033&lt;/TD&gt;
&lt;TD&gt;-109.294&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&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TD&gt;
&lt;TD&gt;241&lt;/TD&gt;
&lt;TD&gt;6/3/2023&lt;/TD&gt;
&lt;TD&gt;53.32764&lt;/TD&gt;
&lt;TD&gt;-156.382&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;524&lt;/TD&gt;
&lt;TD&gt;5/12/2024&lt;/TD&gt;
&lt;TD&gt;55.77004&lt;/TD&gt;
&lt;TD&gt;-163.544&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this code which sorts by Group and Date, and computes the cumulative distance between points for each group at each location, as well as the distance between locations (from prior) for each.&amp;nbsp; The wrench in this is the missing data.&amp;nbsp; I don't want to compute the distance between two locations if they are not successive.&amp;nbsp; For instance, if one location is missing coordinates, then the distance values for that AND the next location should be null, and cumulative held constant until 2 locations AFTER the one with the missing data.&amp;nbsp; How can I modify this code to do that?&amp;nbsp; I also do the same with the dates between locations but there is no missing data to deal with there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sort data=my_data;
by Group Date;
run;

data my_data_new; set my_data;
by group;

cum_dist+geodist(lat,long, lag(lat), lag(long));
if first.group then cum_dist = 0;
run;

Data my_data_new1; set My_data_new;

dist_prev = sum(cum_dist, -lag1(cum_dist));
if first.group then dist_prev = .;

days_prev = abs(dif(Date));
if first.group then days_prev = .;

run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 14:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956189#M373403</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2025-01-15T14:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to deal with missing coordinate data when computing geodistance by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956194#M373404</link>
      <description>I should note that the above code gives a prev_dist value of 0 for the location with missing data and the location after it.  However, since some locations are the same in my actual data, I want to differentiate between 0 actual distance and 0 due to missing data.  I hope that makes sense!</description>
      <pubDate>Wed, 15 Jan 2025 14:33:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956194#M373404</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2025-01-15T14:33:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to deal with missing coordinate data when computing geodistance by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956203#M373405</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264750"&gt;@RandoDando&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you're almost there. Just store the value returned by the GEODIST function in variable &lt;FONT face="courier new,courier"&gt;dist_prev&lt;/FONT&gt; rather than computing&amp;nbsp;&lt;FONT face="courier new,courier"&gt;dist_prev&lt;/FONT&gt;&amp;nbsp;from cumulative distances. You can also omit the second DATA step (which is missing a BY statement!) and do all calculations in the first as shown below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
call streaminit(27182818);
do g=1 to 5;
  date=rand('integer','01JAN2022'd,'31DEC2022'd);
  do location=1 to 30;
    group=byte(64+g);
    date+rand('integer',50);
    if rand('bern',0.7) then do;
      lat=rand('uniform',30,60);
      long=rand('uniform',-165,-95);
    end;
    else call missing(lat, long);
    output;
  end;
end;
format date yymmdd10.;
run;

/* Compute distances, cumulative distances and time differences */

data want;
set have;
by group;
dist_prev=geodist(lat, long, lag(lat), lag(long));
cum_dist+dist_prev;
days_prev=dif(date);
if first.group then do;
  cum_dist = 0;
  call missing(dist_prev, days_prev);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2025 15:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956203#M373405</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-01-15T15:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to deal with missing coordinate data when computing geodistance by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956221#M373410</link>
      <description>&lt;P&gt;OK, was just handed another wrench. How about if I have TWO dates (a start_date and End_date) and I want the days_prev to be the difference between the start date and the end_date of the previous record?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT:&amp;nbsp; got it.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;days_prev = start_date - lag(end_date);
if first.group then days_prev = .;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2025 16:57:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-deal-with-missing-coordinate-data-when-computing/m-p/956221#M373410</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2025-01-15T16:57:54Z</dc:date>
    </item>
  </channel>
</rss>

