## SAS macro for pair distances

Solved
Occasional Contributor
Posts: 6

# SAS macro for pair distances

I try to compute the nearest distance between the trees of a forest for  a set of forests, each forest identified by a number. The input data look like this, where x and y are the coordinates of a tree:

`x y forest

1 3 1

2 3 1

.....

5 6 2

.....`

I would like to have the results saved in one file, also identifying the forest, something like that (other variable can appear too):

`tree1 tree2 distance forest

1 4 24 1

1 15 26 1

.....

1 16 3 2

.......`

I am using the proc variogram and the objective is the output file that computes the pair distances. I have the code working for one forest but I have 600 forests and I would like to automatize the process. I thought that a macro could help. This is what I have so far, but it give me errors as I have identify the forest for which the distances are computed:

%macro repeat;

%do forest= 1 %to 600;

Proc variogram data=data outpair=nndata;

compute outpdistance=15 novar;

coordinates xcoord=x ycoord=y;

run;quit;

%end;

%mend repeat;

%repeat;

Any suggestion on how to create the outpair data set is greatly appreciated.

Best wishes,

Nat

Accepted Solutions
Solution
‎08-16-2013 08:14 AM
Posts: 2,655

## Re: SAS macro for pair distances

Rather than writing a macro loop, why not use BY group processing:

Proc variogram data=data outpair=nndata;

by forest;

compute outpdistance=15 novar;

coordinates xcoord=x ycoord=y;

run;

Steve Denham

All Replies
Super Contributor
Posts: 308

## Re: SAS macro for pair distances

%macro repeat;

%do forest= 1 %to 600;

Proc variogram data=data outpair=nndata;

compute outpdistance=15 novar;

coordinates xcoord=x ycoord=y;

WHERE FOREST = &FOREST ;

run;quit;

%end;

%mend repeat;

%repeat;

Solution
‎08-16-2013 08:14 AM
Posts: 2,655

## Re: SAS macro for pair distances

Rather than writing a macro loop, why not use BY group processing:

Proc variogram data=data outpair=nndata;

by forest;

compute outpdistance=15 novar;

coordinates xcoord=x ycoord=y;

run;

Steve Denham

Occasional Contributor
Posts: 6

## Re: SAS macro for pair distances

Dear Steve,

Thank you very much for your replay. I did not know that by statement can be sued inside variogram procedure. This solved all the problem.

Best wishes,

Nat

Super Contributor
Posts: 308

## Re: SAS macro for pair distances

I thought about the BY statement option but assumed there was some reason why you needed separate outputs for each forest code. The BY statement makes far better sense if you don't need separate output tables.

Occasional Contributor
Posts: 6

## Re: SAS macro for pair distances

Dear Fugue,

Thank you very much for your replay. It worked great, but it saved only the last dataset, as the rests were overwritten by the latest dataset created.  A fast and easy solution was proposed by another user who said to use the by statement, which I did not know that can be used inside variogram procedure.

Best wishes,

Nat

Super Contributor
Posts: 308

## Re: SAS macro for pair distances

Sorry Nat, it was late and I was at home . . . I should have used a double ampersand . . . also, the output statement needed to be modified with a counter (&&forest), otherwise every iteration would overwrite the same dataset.

%macro repeat;

%do forest= 1 %to 600;

Proc variogram data=data outpair=nndata&&forest ;

compute outpdistance=15 novar;

coordinates xcoord=x ycoord=y;

WHERE FOREST = &&FOREST ;

run;quit;

%end;

%mend repeat;

%repeat;

🔒 This topic is solved and locked.