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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

2 REPLIES 2
Rick_SAS
SAS Super FREQ

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

e3flator
Calcite | Level 5

Thank you very much Rick.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1220 views
  • 1 like
  • 2 in conversation