Hi,
I'm trying to update a sas dataset using proc iml but with no success.
his is the code I wrote:
data a1;
a='c1'; a1=1; a2=2; a3=5; wr=3;output;
a='c2';a1=3; a2=1; a3=4; wr=5;output;
run;
proc iml;
edit a1 VAR {A1 A2 A3 WR} ;
read all var {A1 A2 A3} into nn;
read all var { wr} into wr;
print nn;
do i=1 to 2;
do j=1 to 3;
if i^=j then nn[i,j]=nn[i,j]*1/(1-wr/2);
end;
replace point i;
end;
replace ;
print wr nn;
close a1 ;
quit;
But at the end even if the nn matrix is correct a1 dataset is not updated.
I know that my question is rather easy, but I'm new with this procedure.
Thanks in advance
Federica
There are two errors here:
1) The default "range" for the REPLACE stmt is CURRENT, so the second call to REPLACE (outside the loop) isn't doing what you want.
2) The REPLACE stmt replaces variables that are in common to the data set and the IML program. Inside the loop, you are trying to update the A1, A2, and A3 variables, but you haven''t changed those vectors. You are updating the NN matrix, but this matrix is not "linked" or connected to the A1-A3 variables, even though it was created from them.
I find that it is easiest and fastest to use REPLACE ALL rather than REPLACE CURRENT or REPLACE POINT. I recommend that you update the NN matrix, then copy the columns of NN back into A1-A3, then use REPLACE ALL, as follows:
do i=1 to 2;
do j=1 to 3;
if i^=j then nn[i,j]=nn[i,j]*1/(1-wr/2);
end;
end;
A1 = nn[,1]; A2 = nn[,2]; A3 = nn[,3];
replace all var{A1 A2 A3};
There are two errors here:
1) The default "range" for the REPLACE stmt is CURRENT, so the second call to REPLACE (outside the loop) isn't doing what you want.
2) The REPLACE stmt replaces variables that are in common to the data set and the IML program. Inside the loop, you are trying to update the A1, A2, and A3 variables, but you haven''t changed those vectors. You are updating the NN matrix, but this matrix is not "linked" or connected to the A1-A3 variables, even though it was created from them.
I find that it is easiest and fastest to use REPLACE ALL rather than REPLACE CURRENT or REPLACE POINT. I recommend that you update the NN matrix, then copy the columns of NN back into A1-A3, then use REPLACE ALL, as follows:
do i=1 to 2;
do j=1 to 3;
if i^=j then nn[i,j]=nn[i,j]*1/(1-wr/2);
end;
end;
A1 = nn[,1]; A2 = nn[,2]; A3 = nn[,3];
replace all var{A1 A2 A3};
Thank you very much Rick.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.