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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 7310 views
  • 1 like
  • 2 in conversation