BookmarkSubscribeRSS Feed
pdiff
Obsidian | Level 7

Hello,

         I have a large grid of x, y, z format data (800 x 800) . I need to interpolate unknown z values at pre selected (possibly random) x, y points that do not fall on the known x, y coordinates. As far as I can tell, g3grid, krig2d and sim2d will not do this (interpolate missing values). Is there any way to do this?

 

Alternatively, is there a way to overlay the unknown points on the known grid and select the nearest k neighbors to the unknown x,y, points? That would also be an acceptable solution in the case.

 

TIA for any help here.

Bill

14 REPLIES 14
PaigeMiller
Diamond | Level 26
  • Linear  model fit plus interpolation? PROC GLM
  • Polynomial model fit plus interpolation? PROC GLM
  • Many types of spline and non-linear model fit plus interpolation? PROC TRANSREG, PROC TPSPLINE, PROC NLIN, PROC ADAPTIVEREG
 
--
Paige Miller
FreelanceReinh
Jade | Level 19

Hello @pdiff,

 

Did you see the recent thread "Bilinear interpolation in SAS" where different approaches to this problem (e.g., a DATA step) were discussed?

Ksharp
Super User
For you alternative way, you could calculate E-distance between X and Y like:
(y2-y1)^2 + (x2-x1)^2
and pick up the smallest distance 's X and Y ,and lock its Z ?
ballardw
Super User

If you have SAS/Graph licensed and access to  Proc G3Grid that might be another option.

PaigeMiller
Diamond | Level 26

@ballardw wrote:

If you have SAS/Graph licensed and access to  Proc G3Grid that might be another option.


The OP specifically ruled out PROC G3GRID for this task. Is there a way to obtain interpolations from PROC G3GRID?

--
Paige Miller
ballardw
Super User

@PaigeMiller wrote:

@ballardw wrote:

If you have SAS/Graph licensed and access to  Proc G3Grid that might be another option.


The OP specifically ruled out PROC G3GRID for this task. Is there a way to obtain interpolations from PROC G3GRID?


I suspect the OP either did not test G3grid or the data he has to work from does not cover the area well.

From the documentation there are some concerns:

If the points that are generated by the horizontal variables tend to lie along a curve, a poor interpolation or spline can result. In such cases, the vertical variable(s), and one of the horizontal variables should be modeled as a function of the remaining horizontal variable. A scatter plot of the two horizontal variables enable you to determine the appropriate function.

However I suspect any model approach may have a similar problem going much outside of the existing range of values.

The first example from the documentation creates an example input set and the uses the proc.

data nums;
   keep x y z;
   do i=1 to 30;
      x=10*ranuni(33)-5;
      y=10*ranuni(33)-5;
      z=sin(sqrt(x*x+y*y));
      output;
   end;
run;

proc g3grid data=nums out=default;
   grid y*x=z / 
      axis1=-5 to 5 by .5
      axis2=-5 to 5 by .5;
   run;
quit;

It is pretty easy to see that for this example we end up with lots of interpolated Z values from X and Y that do not actually exist in the data. When I ran the NUMS data step above none of the x or y variables have values that were values in the axis statements above but every one has a Z associated. The question might be how good for the intended use. There are a number of interpolation options available but the question may be suitable for specific purpose.

I think the code is simple enough to be worth testing as the AXIS statement will accept a list of values.

proc g3grid data=nums out=list;
   grid y*x=z / 
      axis1=1 2 3
      axis2= 4 5 6;
   run;
quit;

Has output values for y=1 2 3 each paired with x=4 5 6. This might create a few more points than the OP needs but it should not be hard to drop any unneeded ones.

 

 

 

PaigeMiller
Diamond | Level 26

What if the OP wants to interpolate z values for a data set of x,y values, rather than some predefined values?

--
Paige Miller
pdiff
Obsidian | Level 7

Actually, g3grid is the first option I tried and it covers the area quite well. It could do exactly what I desire, but the issue is that the grid axis statements, such as you demonstrate, take only a list of numeric values, not a variable or dataset with the information. The x,y coordinates I need are generated by algorithm (either systematically or randomly). I would need to transfer these to a list, such as a macro variable (or variables depending on how many), to generically insert into a g3grid call.

ballardw
Super User

@pdiff wrote:

Actually, g3grid is the first option I tried and it covers the area quite well. It could do exactly what I desire, but the issue is that the grid axis statements, such as you demonstrate, take only a list of numeric values, not a variable or dataset with the information. The x,y coordinates I need are generated by algorithm (either systematically or randomly). I would need to transfer these to a list, such as a macro variable (or variables depending on how many), to generically insert into a g3grid call.


Generating code values from data is the MACRO language purview. A comma delimited list of values from a data set is relatively easy:

Proc sql noprint;
    select distinct height, weight
       into : heightlist separated by ',' , :weightlist separated by ','
   from sashelp.class
   ;
quit;
%put Heightlist is: &heightlist.;
%put Weightlist is: &weightlist.;

So something like the &heightlist. could be used as: Axis1=&heightlist Axis2 = &weightlist;

PaigeMiller
Diamond | Level 26

Generating code values from data is the MACRO language purview. A comma delimited list of values from a data set is relatively easy:

 

My point of view is that if you have x, y values in a data set, you don't need to turn them into macro variables to obtain interpolations. There are many interpolation methods in SAS that can use the data set directly. And you have a much wider choice of interpolation methods than exist in G3GRID.

 
--
Paige Miller
pdiff
Obsidian | Level 7

I agree, and likely would be more efficient which I would like as the process will eventually be embedded in a bootstrap process.

pdiff
Obsidian | Level 7

Yes, I was just playing with this. when I saw your reply. It works, although g3grid over produces as it is running a full grid pattern and interpolates all axis1 values at all axis2 values. I only need the matched set of axis1, axis2 pairs. I can filter by the actual sample coordinate pairs, but it will take some tinkering with formats and precision as g3grid seems to truncate the x, y values.

pdiff
Obsidian | Level 7

I ruled it out as being unable to interpolate any missing z values in the input dataset. It will, as we've seen, interpolate requested values in an axis option call.

PGStats
Opal | Level 21

The answer I gave here:

 

https://communities.sas.com/t5/SAS-Programming/Bilinear-interpolation-in-SAS/m-p/640174/highlight/tr... 

 

shows how to use the grid statement in proc krige2d to do 2d interpolation at locations specified in a dataset. The observations don't have to be on a regular grid, nor the interpolation locations.

PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 2093 views
  • 3 likes
  • 6 in conversation