Statistical programming, matrix languages, and more

Update a sas dataset with proc iml

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Update a sas dataset with proc iml

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


Accepted Solutions
Solution
‎08-06-2012 09:42 AM
SAS Super FREQ
Posts: 3,236

Re: Update a sas dataset with proc iml

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};

View solution in original post


All Replies
Solution
‎08-06-2012 09:42 AM
SAS Super FREQ
Posts: 3,236

Re: Update a sas dataset with proc iml

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};

Occasional Contributor
Posts: 5

Re: Update a sas dataset with proc iml

Thank you very much Rick.

Post a Question
Discussion Stats
  • 2 replies
  • 392 views
  • 1 like
  • 2 in conversation