<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Defining weight matrix for spatialprog procedure in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Defining-weight-matrix-for-spatialprog-procedure/m-p/639608#M190313</link>
    <description>&lt;P&gt;Hi Folks:&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to understand where I'm doing wrong and how to correct the macro or the approach i'm using to create weigtht matrix datasets to model a spatially auto-correlated data using proc spatialreg?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My goal is to model the data as following by SAR method.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc spatialreg data=outcome_data /*250*/ Wmat=Wmatrix_compact;
  model health_outcome=kindergarten_count/type=SAR;
  spatialid code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I tried to define a spatial weight matrix based on&amp;nbsp;&lt;CODE class=" language-sas"&gt;mapsgfk.south_korea&lt;/CODE&gt;&amp;nbsp;data using the Macro presented in SUGI paper:&amp;nbsp;&lt;A href="https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2481-2018.pdf" target="_blank" rel="noopener"&gt;https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2481-2018.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro outputs three matrix datasets with number of rows: Wmatrix /*Nobs=241*/&lt;/P&gt;
&lt;P&gt;Wmatrix_sdr /*Nobs=241*/, and&amp;nbsp;Wmatrix_compact /*Nobs=1,284*/.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Spatial ID (code) that I created in korean map data has 250 level and I organized my primary health outcome data to N=250 rows as well. These 250 indicates the unique combination of province and district in South Korea. However, all weight matrix datasets were created with a reduced number of rows in Wmatrix /*Nobs=241*/, Wmatrix_sdr /*Nobs=241*/ and&amp;nbsp;Wmatrix_compact /*Nobs=1,284*/ compared to N=250.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Spatialreg procedure doesn't fit the model with any of these three matrix weight datasets. Below is the error example when I use wmatrix_compact.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: The data sets WORK.OUTCOME_DATA and WORK.WMATRIX_COMPACT do not have the same number of spatial IDs.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How I created spatial ID in korean map data and ran Macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*CREATING SPATIAL ID FOR MAP DATA*/;
proc sort data=mapsgfk.south_korea; /**/ 
by ID1 ID;  
data south_korea; /*119,675*/ set mapsgfk.south_korea;
     by ID1 ID;
     retain code 0;
     if first.ID then code+1;
run; 

%WMATRIX(map=south_korea,id=code,lat=y,long=x,type=contiguity,out=neighbor_kor);

%macro Wmatrix(map=,id=,lat=,long=,type=contiguity,centroid_lat=,
centroid_long=, distance=,neighbors=,out=neighbor);

%if %upcase(&amp;amp;type)=CONTIGUITY %then %do;
%if &amp;amp;neighbors = %then %do;
/********Defining the Neighborhood for contiguity ***********/
proc sort data=&amp;amp;map out=&amp;amp;map.2 nodupkey; by &amp;amp;long &amp;amp;lat &amp;amp;id;run;
data _hashmap_ (keep=&amp;amp;long &amp;amp;lat z &amp;amp;id);
retain z;
set &amp;amp;map.2(where=(&amp;amp;long NE . and &amp;amp;lat NE .));
by &amp;amp;long &amp;amp;lat;
if first.&amp;amp;long or first.&amp;amp;lat then z=1;
z=z+1;
run;
proc means data=_hashmap_ noprint;
output out=maxz max(z)=mz; run;
/*put the maximum value of z into macro variable maxnb*/
data _null_;
set maxz;
call symputx("maxnb",mz);
run;
%put &amp;amp;maxnb;
proc sort data=&amp;amp;map.2; by &amp;amp;id;run;
data nonb (keep=myid);
/*length &amp;amp;long &amp;amp;lat z 8; format &amp;amp;id 16.;*/
if _n_=1 then do;
/*this hash object holds a second copy of the entire map for comparison*/
declare hash fc(dataset: "_hashmap_");
fc.definekey("&amp;amp;long","&amp;amp;lat","z");
fc.definedata("&amp;amp;long","&amp;amp;lat","z","&amp;amp;id");
fc.definedone();
call missing (&amp;amp;long,&amp;amp;lat,z,&amp;amp;id);
/*this hash object will hold the rook neighbors for each area: they have two
points in common*/
declare hash rook();
rook.definekey("&amp;amp;id","myid");
rook.definedata("&amp;amp;id","myid");
rook.definedone();
end;
/*this hash object holds the bishop neighbors for each area: they have a
point
in common*/
declare hash bishop();
bishop.definekey("&amp;amp;id","myid");
bishop.definedata("&amp;amp;id","myid");
bishop.definedone();
foundnb="N";
do until (last.myid);
set &amp;amp;map.2 (keep=&amp;amp;id &amp;amp;long &amp;amp;lat rename=(&amp;amp;id=myid &amp;amp;long=myx &amp;amp;lat=myy)
where=(myx NE .
and myy NE .)) end=eof;
by myid;
do n=1 to &amp;amp;maxnb.; /*this is max number of points in common =max z*/
rc=fc.find(key:myx, key:myy, key:n);
if rc=0 and myid NE &amp;amp;id then do;
nbrc=rook.check(key:&amp;amp;id, key:myid);
if nbrc=0 then do;
rc2=rook.add(key:&amp;amp;id, key:myid, data:&amp;amp;id, data:myid);
foundnb="Y";
end;
else rc1=rook.add(key:&amp;amp;id, key:myid, data:&amp;amp;id, data:myid);
end;
end;*do &amp;amp;maxnb.;
end;*end DOW loop;
if foundnb="N" then output nonb;
if eof then rook.output(dataset:"&amp;amp;out");
run;
proc sort data=&amp;amp;out;by &amp;amp;id myid;run;
proc sort data=&amp;amp;out(keep=&amp;amp;id) nodupkey out=_tab_;by &amp;amp;id;run;
data _tab_;set _tab_;idi=_n_;run;
proc sort data=&amp;amp;out;by &amp;amp;id;run;
data _tab2_;merge &amp;amp;out _tab_;by &amp;amp;id;run;
proc sort data=_tab2_;by myid;run;
data _tab2_;merge _tab2_ _tab_(rename=(idi=idj &amp;amp;id=myid));by myid;run;
proc sort data=_tab2_;by &amp;amp;id;run;
proc sql;drop table _tab_,_hashmap_,Nonb,Maxz,&amp;amp;map.2;quit;
data Wmatrix_compact;set &amp;amp;out(rename=myid=c&amp;amp;id);
value=1;
run;
%end;
%else %do;
%if &amp;amp;centroid_lat= or &amp;amp;centroid_long= %then %do;
%annomac;
proc sort data=&amp;amp;map(keep=&amp;amp;id &amp;amp;lat &amp;amp;long rename=(&amp;amp;long=y &amp;amp;lat=x)) out=_&amp;amp;map;by
&amp;amp;id;run;
%centroid(_&amp;amp;map,&amp;amp;out,&amp;amp;id);
proc sql;drop table _&amp;amp;map;quit;
%end;
%else %do;
data &amp;amp;out;set &amp;amp;map(keep=&amp;amp;id &amp;amp;centroid_lat &amp;amp;centroid_long
rename=(&amp;amp;centroid_long=y &amp;amp;centroid_lat=x));run;
%end;
proc iml;
use &amp;amp;out;
read all var{x y} into COORD;
n=nrow(coord[,1]);
*print n;
d=j(1,3,0);
nome={"idi" "idj" "d"};
create _dist_ from d[colname=nome];
do i=1 to n;
do j=1 to n;
d[1]=i;
d[2]=j;
d[3]=sqrt((COORD[i,1]-COORD[j,1])**2+(COORD[i,2]-COORD[j,2])**2);
append from d;
end;
end;
close _dist_;
quit;
data _dist_;set _dist_;if d=0 then delete;run;
proc sort data=_dist_;by idi d;run;
data _dist_;retain seq 0;set _dist_;by idi;
if first.idi then seq=1;
else seq+1;
run;
data _tab2_;set _dist_(where=(seq&amp;lt;=&amp;amp;neighbors));run;
data &amp;amp;out;set _tab2_;run;
data Wmatrix_compact;set &amp;amp;out;
rename idi=&amp;amp;id idj=c&amp;amp;id;value=1;
drop seq d;
run;
proc sql;drop table _dist_;quit;
%end;
/********creating W Matrix ***********/
proc iml;
use _tab2_ var{idi idj};
read all;
n=max(idj);
w=j(n,n,0);
do h=1 to nrow(idi);
w[idi[h],idj[h]]=1;
end;
wpdr=j(n,n,0);
soma=j(n,1,0);
do i=1 to n;
do j=1 to n;
soma[i]=soma[i]+w[i,j];
end;
end;
do i=1 to n;
do j=1 to n;
if soma[i]=0 then wpdr[i,j]=0;
else wpdr[i,j]=w[i,j]/soma[i];
end;
end;
create Wmatrix from w;
append from w;
create Wmatrix_sdr from wpdr;
append from wpdr;
quit;
proc sql;drop table _tab2_;quit;
%end;
%else %if %upcase(&amp;amp;type)=DISTANCE %then %do;
%if &amp;amp;centroid_lat= or &amp;amp;centroid_long= %then %do;
%annomac;
proc sort data=&amp;amp;map(keep=&amp;amp;id &amp;amp;lat &amp;amp;long rename=(&amp;amp;long=y &amp;amp;lat=x)) out=_&amp;amp;map;by
&amp;amp;id;run;
%centroid(_&amp;amp;map,&amp;amp;out,&amp;amp;id);
proc sql;drop table _&amp;amp;map;quit;
%end;
%else %do;
data &amp;amp;out;set &amp;amp;map(keep=&amp;amp;id &amp;amp;centroid_lat &amp;amp;centroid_long
rename=(&amp;amp;centroid_long=y &amp;amp;centroid_lat=x));run;
%end;
proc iml;
use &amp;amp;out;
read all var{x y} into COORD;
n=nrow(coord[,1]);
*print n;
d=j(1,3,0);
nome={"idi" "idj" "d"};
create _dist_ from d[colname=nome];
do i=1 to n;
do j=i+1 to n;
d[1]=i;
d[2]=j;
d[3]=sqrt((COORD[i,1]-COORD[j,1])**2+(COORD[i,2]-COORD[j,2])**2);
append from d;
end;
end;
close _dist_;
quit;
data &amp;amp;out;set _dist_;run;
proc sql;drop table _dist_;quit;
data Wmatrix_compact;
set &amp;amp;out(rename=(idi=&amp;amp;id idj=c&amp;amp;id)) &amp;amp;out(rename=(idi=c&amp;amp;id idj=&amp;amp;id));
%if &amp;amp;distance= %then %do;
value=1/d;
%end;
%else %do;
if d&amp;lt;=&amp;amp;distance then value=1;
if value=. then delete;
%end;
keep &amp;amp;id c&amp;amp;id value;
run;
proc sort data=Wmatrix_compact;by &amp;amp;id c&amp;amp;id;run;
/********creating W Matrix ***********/
proc iml;
use &amp;amp;out var{idi idj d};
read all;
n=max(idj);
w=j(n,n,0);
do h=1 to nrow(idi);
%if &amp;amp;distance= %then %do;
w[idi[h],idj[h]]=1/d[h];
w[idj[h],idi[h]]=1/d[h];
%end;
%else %do;
if d[h]&amp;lt;=&amp;amp;distance then do;
w[idi[h],idj[h]]=1;
w[idj[h],idi[h]]=1;
end;
%end;
end;
wpdr=j(n,n,0);
soma=j(n,1,0);
do i=1 to n;
do j=1 to n;
soma[i]=soma[i]+w[i,j];
end;
end;
do i=1 to n;
do j=1 to n;
if soma[i]=0 then wpdr[i,j]=0;
else wpdr[i,j]=w[i,j]/soma[i];
end;
end;
create Wmatrix from w;
append from w;
create Wmatrix_sdr from wpdr;
append from wpdr;
quit;
%end;
%mend Wmatrix;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Apr 2020 15:10:11 GMT</pubDate>
    <dc:creator>Cruise</dc:creator>
    <dc:date>2020-04-14T15:10:11Z</dc:date>
    <item>
      <title>Defining weight matrix for spatialprog procedure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-weight-matrix-for-spatialprog-procedure/m-p/639608#M190313</link>
      <description>&lt;P&gt;Hi Folks:&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to understand where I'm doing wrong and how to correct the macro or the approach i'm using to create weigtht matrix datasets to model a spatially auto-correlated data using proc spatialreg?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My goal is to model the data as following by SAR method.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc spatialreg data=outcome_data /*250*/ Wmat=Wmatrix_compact;
  model health_outcome=kindergarten_count/type=SAR;
  spatialid code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I tried to define a spatial weight matrix based on&amp;nbsp;&lt;CODE class=" language-sas"&gt;mapsgfk.south_korea&lt;/CODE&gt;&amp;nbsp;data using the Macro presented in SUGI paper:&amp;nbsp;&lt;A href="https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2481-2018.pdf" target="_blank" rel="noopener"&gt;https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2481-2018.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro outputs three matrix datasets with number of rows: Wmatrix /*Nobs=241*/&lt;/P&gt;
&lt;P&gt;Wmatrix_sdr /*Nobs=241*/, and&amp;nbsp;Wmatrix_compact /*Nobs=1,284*/.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Spatial ID (code) that I created in korean map data has 250 level and I organized my primary health outcome data to N=250 rows as well. These 250 indicates the unique combination of province and district in South Korea. However, all weight matrix datasets were created with a reduced number of rows in Wmatrix /*Nobs=241*/, Wmatrix_sdr /*Nobs=241*/ and&amp;nbsp;Wmatrix_compact /*Nobs=1,284*/ compared to N=250.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Spatialreg procedure doesn't fit the model with any of these three matrix weight datasets. Below is the error example when I use wmatrix_compact.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: The data sets WORK.OUTCOME_DATA and WORK.WMATRIX_COMPACT do not have the same number of spatial IDs.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How I created spatial ID in korean map data and ran Macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*CREATING SPATIAL ID FOR MAP DATA*/;
proc sort data=mapsgfk.south_korea; /**/ 
by ID1 ID;  
data south_korea; /*119,675*/ set mapsgfk.south_korea;
     by ID1 ID;
     retain code 0;
     if first.ID then code+1;
run; 

%WMATRIX(map=south_korea,id=code,lat=y,long=x,type=contiguity,out=neighbor_kor);

%macro Wmatrix(map=,id=,lat=,long=,type=contiguity,centroid_lat=,
centroid_long=, distance=,neighbors=,out=neighbor);

%if %upcase(&amp;amp;type)=CONTIGUITY %then %do;
%if &amp;amp;neighbors = %then %do;
/********Defining the Neighborhood for contiguity ***********/
proc sort data=&amp;amp;map out=&amp;amp;map.2 nodupkey; by &amp;amp;long &amp;amp;lat &amp;amp;id;run;
data _hashmap_ (keep=&amp;amp;long &amp;amp;lat z &amp;amp;id);
retain z;
set &amp;amp;map.2(where=(&amp;amp;long NE . and &amp;amp;lat NE .));
by &amp;amp;long &amp;amp;lat;
if first.&amp;amp;long or first.&amp;amp;lat then z=1;
z=z+1;
run;
proc means data=_hashmap_ noprint;
output out=maxz max(z)=mz; run;
/*put the maximum value of z into macro variable maxnb*/
data _null_;
set maxz;
call symputx("maxnb",mz);
run;
%put &amp;amp;maxnb;
proc sort data=&amp;amp;map.2; by &amp;amp;id;run;
data nonb (keep=myid);
/*length &amp;amp;long &amp;amp;lat z 8; format &amp;amp;id 16.;*/
if _n_=1 then do;
/*this hash object holds a second copy of the entire map for comparison*/
declare hash fc(dataset: "_hashmap_");
fc.definekey("&amp;amp;long","&amp;amp;lat","z");
fc.definedata("&amp;amp;long","&amp;amp;lat","z","&amp;amp;id");
fc.definedone();
call missing (&amp;amp;long,&amp;amp;lat,z,&amp;amp;id);
/*this hash object will hold the rook neighbors for each area: they have two
points in common*/
declare hash rook();
rook.definekey("&amp;amp;id","myid");
rook.definedata("&amp;amp;id","myid");
rook.definedone();
end;
/*this hash object holds the bishop neighbors for each area: they have a
point
in common*/
declare hash bishop();
bishop.definekey("&amp;amp;id","myid");
bishop.definedata("&amp;amp;id","myid");
bishop.definedone();
foundnb="N";
do until (last.myid);
set &amp;amp;map.2 (keep=&amp;amp;id &amp;amp;long &amp;amp;lat rename=(&amp;amp;id=myid &amp;amp;long=myx &amp;amp;lat=myy)
where=(myx NE .
and myy NE .)) end=eof;
by myid;
do n=1 to &amp;amp;maxnb.; /*this is max number of points in common =max z*/
rc=fc.find(key:myx, key:myy, key:n);
if rc=0 and myid NE &amp;amp;id then do;
nbrc=rook.check(key:&amp;amp;id, key:myid);
if nbrc=0 then do;
rc2=rook.add(key:&amp;amp;id, key:myid, data:&amp;amp;id, data:myid);
foundnb="Y";
end;
else rc1=rook.add(key:&amp;amp;id, key:myid, data:&amp;amp;id, data:myid);
end;
end;*do &amp;amp;maxnb.;
end;*end DOW loop;
if foundnb="N" then output nonb;
if eof then rook.output(dataset:"&amp;amp;out");
run;
proc sort data=&amp;amp;out;by &amp;amp;id myid;run;
proc sort data=&amp;amp;out(keep=&amp;amp;id) nodupkey out=_tab_;by &amp;amp;id;run;
data _tab_;set _tab_;idi=_n_;run;
proc sort data=&amp;amp;out;by &amp;amp;id;run;
data _tab2_;merge &amp;amp;out _tab_;by &amp;amp;id;run;
proc sort data=_tab2_;by myid;run;
data _tab2_;merge _tab2_ _tab_(rename=(idi=idj &amp;amp;id=myid));by myid;run;
proc sort data=_tab2_;by &amp;amp;id;run;
proc sql;drop table _tab_,_hashmap_,Nonb,Maxz,&amp;amp;map.2;quit;
data Wmatrix_compact;set &amp;amp;out(rename=myid=c&amp;amp;id);
value=1;
run;
%end;
%else %do;
%if &amp;amp;centroid_lat= or &amp;amp;centroid_long= %then %do;
%annomac;
proc sort data=&amp;amp;map(keep=&amp;amp;id &amp;amp;lat &amp;amp;long rename=(&amp;amp;long=y &amp;amp;lat=x)) out=_&amp;amp;map;by
&amp;amp;id;run;
%centroid(_&amp;amp;map,&amp;amp;out,&amp;amp;id);
proc sql;drop table _&amp;amp;map;quit;
%end;
%else %do;
data &amp;amp;out;set &amp;amp;map(keep=&amp;amp;id &amp;amp;centroid_lat &amp;amp;centroid_long
rename=(&amp;amp;centroid_long=y &amp;amp;centroid_lat=x));run;
%end;
proc iml;
use &amp;amp;out;
read all var{x y} into COORD;
n=nrow(coord[,1]);
*print n;
d=j(1,3,0);
nome={"idi" "idj" "d"};
create _dist_ from d[colname=nome];
do i=1 to n;
do j=1 to n;
d[1]=i;
d[2]=j;
d[3]=sqrt((COORD[i,1]-COORD[j,1])**2+(COORD[i,2]-COORD[j,2])**2);
append from d;
end;
end;
close _dist_;
quit;
data _dist_;set _dist_;if d=0 then delete;run;
proc sort data=_dist_;by idi d;run;
data _dist_;retain seq 0;set _dist_;by idi;
if first.idi then seq=1;
else seq+1;
run;
data _tab2_;set _dist_(where=(seq&amp;lt;=&amp;amp;neighbors));run;
data &amp;amp;out;set _tab2_;run;
data Wmatrix_compact;set &amp;amp;out;
rename idi=&amp;amp;id idj=c&amp;amp;id;value=1;
drop seq d;
run;
proc sql;drop table _dist_;quit;
%end;
/********creating W Matrix ***********/
proc iml;
use _tab2_ var{idi idj};
read all;
n=max(idj);
w=j(n,n,0);
do h=1 to nrow(idi);
w[idi[h],idj[h]]=1;
end;
wpdr=j(n,n,0);
soma=j(n,1,0);
do i=1 to n;
do j=1 to n;
soma[i]=soma[i]+w[i,j];
end;
end;
do i=1 to n;
do j=1 to n;
if soma[i]=0 then wpdr[i,j]=0;
else wpdr[i,j]=w[i,j]/soma[i];
end;
end;
create Wmatrix from w;
append from w;
create Wmatrix_sdr from wpdr;
append from wpdr;
quit;
proc sql;drop table _tab2_;quit;
%end;
%else %if %upcase(&amp;amp;type)=DISTANCE %then %do;
%if &amp;amp;centroid_lat= or &amp;amp;centroid_long= %then %do;
%annomac;
proc sort data=&amp;amp;map(keep=&amp;amp;id &amp;amp;lat &amp;amp;long rename=(&amp;amp;long=y &amp;amp;lat=x)) out=_&amp;amp;map;by
&amp;amp;id;run;
%centroid(_&amp;amp;map,&amp;amp;out,&amp;amp;id);
proc sql;drop table _&amp;amp;map;quit;
%end;
%else %do;
data &amp;amp;out;set &amp;amp;map(keep=&amp;amp;id &amp;amp;centroid_lat &amp;amp;centroid_long
rename=(&amp;amp;centroid_long=y &amp;amp;centroid_lat=x));run;
%end;
proc iml;
use &amp;amp;out;
read all var{x y} into COORD;
n=nrow(coord[,1]);
*print n;
d=j(1,3,0);
nome={"idi" "idj" "d"};
create _dist_ from d[colname=nome];
do i=1 to n;
do j=i+1 to n;
d[1]=i;
d[2]=j;
d[3]=sqrt((COORD[i,1]-COORD[j,1])**2+(COORD[i,2]-COORD[j,2])**2);
append from d;
end;
end;
close _dist_;
quit;
data &amp;amp;out;set _dist_;run;
proc sql;drop table _dist_;quit;
data Wmatrix_compact;
set &amp;amp;out(rename=(idi=&amp;amp;id idj=c&amp;amp;id)) &amp;amp;out(rename=(idi=c&amp;amp;id idj=&amp;amp;id));
%if &amp;amp;distance= %then %do;
value=1/d;
%end;
%else %do;
if d&amp;lt;=&amp;amp;distance then value=1;
if value=. then delete;
%end;
keep &amp;amp;id c&amp;amp;id value;
run;
proc sort data=Wmatrix_compact;by &amp;amp;id c&amp;amp;id;run;
/********creating W Matrix ***********/
proc iml;
use &amp;amp;out var{idi idj d};
read all;
n=max(idj);
w=j(n,n,0);
do h=1 to nrow(idi);
%if &amp;amp;distance= %then %do;
w[idi[h],idj[h]]=1/d[h];
w[idj[h],idi[h]]=1/d[h];
%end;
%else %do;
if d[h]&amp;lt;=&amp;amp;distance then do;
w[idi[h],idj[h]]=1;
w[idj[h],idi[h]]=1;
end;
%end;
end;
wpdr=j(n,n,0);
soma=j(n,1,0);
do i=1 to n;
do j=1 to n;
soma[i]=soma[i]+w[i,j];
end;
end;
do i=1 to n;
do j=1 to n;
if soma[i]=0 then wpdr[i,j]=0;
else wpdr[i,j]=w[i,j]/soma[i];
end;
end;
create Wmatrix from w;
append from w;
create Wmatrix_sdr from wpdr;
append from wpdr;
quit;
%end;
%mend Wmatrix;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 15:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-weight-matrix-for-spatialprog-procedure/m-p/639608#M190313</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2020-04-14T15:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: Defining weight matrix for spatialprog procedure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-weight-matrix-for-spatialprog-procedure/m-p/639841#M190414</link>
      <description>&lt;P&gt;The problem is solved when 'distance' option specified in the Macro. Matrix created had reduced rows because contiguity option discarded islands that didn't share land-borders with other cities.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%WMATRIX(map=south_korea,id=code,lat=y,long=x,type=distance,out=neighbor_kor);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Apr 2020 17:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-weight-matrix-for-spatialprog-procedure/m-p/639841#M190414</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2020-04-14T17:10:18Z</dc:date>
    </item>
  </channel>
</rss>

