BookmarkSubscribeRSS Feed
DJ20
Obsidian | Level 7

Hi everyone,

 

 

I am using an if statement in SAS/IML. I want the program to do nothing if b = 1 else to run some calculations. Is there a way to tell SAS to do nothing in the first part of the if statement as follows:

 

if b = 1

 then do (nothing)

   else do i = 1 to b;

     .

     .

     .

end;

 

I have tried to do it the other way around:

 

if b ~= 1

  then do i = 1 to b;

     .

     .

     .

end;

else do nothing!

 

However SAS is not recognizing the not equal (~=) sign. I am not sure if its the correct operator anyway.

 

Any suggestions would be greatly appreciated.

 

regrds,

DJ

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Change your logic, only take action if b>1

 

if b > 1 then do i = 1 to b;

     .

     .

     .

end;

 

 

In PROC IML, not equal is ^= 

--
Paige Miller
DJ20
Obsidian | Level 7

hi @PaigeMiller 

 

thanks for your reply.

 

I was trying to add the values of each column in a matrix if the number of columns, b, is greater than one. 

 

What I did is I specified an array TB = j(b,1,0);

 

then I added my IF statement 

if b > 1 then do i = 1 to b;

     .

     .

     .

end;
print TB;

 

because I initially sat the array entries to 0's when b is not equal to 1 it prints the value of 0 for TB but I want it to print nothing. I want it to ignore the calculations if b is greater than 1.

 

thanks,

DJ

PaigeMiller
Diamond | Level 26

I am having problems understanding your comments here, it's not clear to me if you are still having a problem or if you are just making comments. Can you please clarify?

 

You don't need loops to sum columns in PROC IML.

https://documentation.sas.com/?docsetId=imlug&docsetTarget=imlug_workmatrix_sect025.htm&docsetVersio...

--
Paige Miller
DJ20
Obsidian | Level 7

Hi @PaigeMiller,

 

 

Ignore the fact that I am using a loop I can change that. In fact I did change it to the following:

 

if b > 1
then TB = data[+,];
print TB;

 

I am adding the elements of each column only if the number of columns, b, is greater than 1. The IF statement I used works however when the number of columns = 1, its giving me an error message as I have asked the program to print TB however haven't told the program what to do if b < 1. So it doesn't know what to print for TB.

 

ERROR: Matrix TB has not been set to a value.

 

obviously, if I take the last line off (print TB;) the error disappears.

 

 

I hope you understand my problem now.

Thanks,

DJ

PaigeMiller
Diamond | Level 26

A simple fix:

 

if b > 1 then do;
    TB = data[+,];
    print TB;
end;
--
Paige Miller
DJ20
Obsidian | Level 7

hi @PaigeMiller 

 

Great, that works. Thanks a lot mate.

 

regards,

DJ