Programming the statistical procedures from SAS

Similating coordinates and calculating various functions

Reply
Super Contributor
Posts: 256

Similating coordinates and calculating various functions

I want to be able to simulate 8 random xy coordinates (limiting the max x and y to 1000 each and only allowing integers) and then be able to do the following:

Calculate all the distances between all the points. For 8 points there will be n(n-1)/2 distances so 28 distances. I want to calculate the mean and standard deviation of the distances.

Calculate the length of every possible route consisting of a path between one of the points (called the start point) and every other point returning to the original point (travelling salesman like situation) and work out the minimum distance.

Calculate the distance from the start point to the furthest point from the start point

Calculate the distance of the perimeter that consists of the outer points of the shape

I know there is a lot here but I'm trying to start off by simulating 8 random numbers. The code below simulates 26 coordinates and the calculates the distance between every point but I want the points to be random.

data Obs;
do x=0 to 5;
   do y=0 to 5;
         output;
   end;
end;
run;

proc distance data=Obs out=Dist method=Euclid nostd;
   var interval(x y);
run;

Thanks

Grand Advisor
Posts: 10,052

Re: Similating coordinates and calculating various functions

You say 8 random numbers but generate 36?

This will generate random points. Change I get the number of pairs you want.

data obs;

   do i= 1 to 36;

      x= round(1000* ranuni(123),1);

      y= round(1000* ranuni(789),1);

      output;

   end;

run;

good luck with the rest.

Grand Advisor
Posts: 16,908

Re: Similating coordinates and calculating various functions

Once you have your simulated data you can do a cross join and then you can do your stats.

Building on @ballardw code

data obs;

   do i= 1 to 8;

      x= round(1000* ranuni(123),1);

      y= round(1000* ranuni(789),1);

      output;

   end;

run;

proc sql;

create table xdistance as

select a.x as x1, a.y as y1,

b.x as x2, b.y as y2

from obs as a

cross join obs as b

where a.i<b.i;

quit;

Super Contributor
Posts: 256

Re: Similating coordinates and calculating various functions

Thanks Reeza

If I chose a START vector, say 750,321 would I need to use arrays to create a table with 18 fields (x1 y1 x2 y2 ... x8 y8 x1y1) which showed every combination of the 8 coordinates conditional on the first number being 750,321. I would then create 19/20th fields with 750,321 in every row. I'm trying to simulate a situation where each coordinate represents a city (so 8 cities) and I want to work out the quickest route if you were to start at 750,321 and visit all cities before returning to 750,321. There are (n-1)!/2 possible routs so 2520.

Once I have my table in the above format I will calculate the total distance travelled for each of the 2520 possible routes. The smallest one will be the optimal route.

Grand Advisor
Posts: 16,908

Re: Similating coordinates and calculating various functions

You could use arrays...

SAS OPTGRAPH is designed exactly for this type of analysis.

SAS(R) OPTGRAPH Procedure 14.1: Graph Algorithms and Network Analysis

Grand Advisor
Posts: 10,052

Re: Similating coordinates and calculating various functions

Ask a Question
Discussion stats
  • 5 replies
  • 294 views
  • 0 likes
  • 3 in conversation