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
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
Add a where clause?
%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;
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
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
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.
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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.