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
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 ^=
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
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.
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
A simple fix:
if b > 1 then do;
TB = data[+,];
print TB;
end;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.