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

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

1 ACCEPTED SOLUTION

Accepted Solutions
SteveDenham
Jade | Level 19

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

View solution in original post

6 REPLIES 6
Fugue
Quartz | Level 8

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;

SteveDenham
Jade | Level 19

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

NatBender
Calcite | Level 5

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

Fugue
Quartz | Level 8

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.

NatBender
Calcite | Level 5

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

Fugue
Quartz | Level 8

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 673 views
  • 4 likes
  • 3 in conversation