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

Hi

I need help with an if statement

tables is as follows

ID.        Sum.         Amt.           Results

A1.        120.               119.                   A

a2.         125.              130.                    A

a3.         126.              126.                     C

iif sum =. Then results. "Notcompleted";

if sum >1  and amt  ne sum = "A";

if order =. And plus money => 1 then  sum= "e";

else sum ="c"

the issue I'm having is the if statement for A some number greater than 0 has to be in Sum an a number has to be I. amt to do comparison of <> the 2 if statements work thanks again

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The expression "greater or less than" still has no meaning in English.

As to the other conditions then you need to be a little clearer about what conditions take precedence.  You also need to construct your IF/THEN/ELSE conditions so that clearly reflect what you want. In your original pseudo code example you had three independent IF statements. In that case it could be possible for the first to be true and set one value into the RESULTS variable and then one of the other expressions to also be true and cause the value to be over written.  The original code also seemed to want to make changes to more than one variable.  Is that what you want?  If want to change two different variables then you will need to two separate sets IF/THEN/ELSE logic trees.  Also if looks like you might want to change the value of one of the variables SUM that is used in the logic for setting RESULTS variable.  If so then you need to be clear about which value of SUM should be used in the calculate of RESULTS.  You also seem to have referenced a variable that is not show in the sample data.  What do you mean by PLUS$ or "plus money"? Note that neither form is valid as a variable name.  You also need to consider other cases.  What happens when SUM is not missing but less than 1?

One way to test is to construct example data that is representative of every possible combination and make sure the logic works for those.

data have ;

  input id $ sum amt expected $ ;

cards;

a1 120 119 A

a2 125 130 A

a3 126 126 C

a4   . 100 N

a5 100   . E

a6   .   . ?

a7   0 0  ?

run;

data want ;

  set have;

  select ;

    when (sum=. and amt > 1) result='N';

    when (amt=. and sum > 1) result='E';

    when (sum = amt) result='C';

    when (sum < amt or sum > amt) result='A';

    otherwise result='U';

  end;

run;

proc print;

run;

id sum    amt    expected result

a1 120    119       A          A

a2 125    130       A          A

a3 126    126       C          C

a4   .    100       N          N

a5 100      .       E          E

a6   .      .       ?          C

a7   0      0       ?          C


View solution in original post

8 REPLIES 8
ballardw
Super User

iif sum =. Then results. "Notcompleted";

if sum >1  and amt  ne sum = "A";

if order =. And plus money => 1 then  sum= "e";

else sum ="c"

None of this code is syntactically correct.

If sum = . then Results='Notcompleted';

if sum>1 and amt ne sum then Results='A'; /* assuming you want to assign to the previously mentioned variable*/

if order =. And plus money => 1 then  sum= "e";

I have no idea what the above statement is supposed to mean. Sum cannot be 'e' as the comparisons above make it appear that sum is a number. Same for the else sum="c" .

Tom
Super User Tom
Super User

What is the actual question here?  From the subject perhaps you are just confused about SAS operators?

In normal SAS statements like IF the <> operator is the binary MAXimum operator.  So 4 <> 5 would result in 5 since 5 is greater than 4.

If you want a not equal operator you can use ^= or NE .

BETO
Fluorite | Level 6

Sorry i provided data that confuse my question.

my table

sUm     Amt.     Results

124.       125.        A

145.         130.      A

my attempt is to determine if "sum "is greater or lesser than than "amt ".. If it is than its A ....if it's not the other if statements that I have will proceed...The one giving me issue is greater and lesser

thanks again

Tom
Super User Tom
Super User

What do you mean by "greater or lesser than"? It can't be both at once.

If we ignore missing values then you test for all three possible relationships with code like this:

if sum > amt then results='A';

else if sum < amt then results='A';

else results='B';

Which is more clearly/easily written as

if sum = amt then results='B';

else results='A';

BETO
Fluorite | Level 6

HI Tom ,

thanks for for the quick response  I actually have  4 if statement to take into consideration every possibly  for example

sum=. That gives me Results =notcompleted

order=. But plus$ is > 1. than results = wrong type

fOr A option to work the tech had to put something in sum and he had to put something in Amt if they are equal than it was completed if I use Not equal it will use In the results " wrong type" because sum is the sum of order and Plus$...

so I need to look at the sum to see if its greater or less than Amt ...I hope that makes sense

Tom
Super User Tom
Super User

The expression "greater or less than" still has no meaning in English.

As to the other conditions then you need to be a little clearer about what conditions take precedence.  You also need to construct your IF/THEN/ELSE conditions so that clearly reflect what you want. In your original pseudo code example you had three independent IF statements. In that case it could be possible for the first to be true and set one value into the RESULTS variable and then one of the other expressions to also be true and cause the value to be over written.  The original code also seemed to want to make changes to more than one variable.  Is that what you want?  If want to change two different variables then you will need to two separate sets IF/THEN/ELSE logic trees.  Also if looks like you might want to change the value of one of the variables SUM that is used in the logic for setting RESULTS variable.  If so then you need to be clear about which value of SUM should be used in the calculate of RESULTS.  You also seem to have referenced a variable that is not show in the sample data.  What do you mean by PLUS$ or "plus money"? Note that neither form is valid as a variable name.  You also need to consider other cases.  What happens when SUM is not missing but less than 1?

One way to test is to construct example data that is representative of every possible combination and make sure the logic works for those.

data have ;

  input id $ sum amt expected $ ;

cards;

a1 120 119 A

a2 125 130 A

a3 126 126 C

a4   . 100 N

a5 100   . E

a6   .   . ?

a7   0 0  ?

run;

data want ;

  set have;

  select ;

    when (sum=. and amt > 1) result='N';

    when (amt=. and sum > 1) result='E';

    when (sum = amt) result='C';

    when (sum < amt or sum > amt) result='A';

    otherwise result='U';

  end;

run;

proc print;

run;

id sum    amt    expected result

a1 120    119       A          A

a2 125    130       A          A

a3 126    126       C          C

a4   .    100       N          N

a5 100      .       E          E

a6   .      .       ?          C

a7   0      0       ?          C


BETO
Fluorite | Level 6

Thank you Tom, I was not aware of the  When statement I used your example an tweak it to make it work for me and it did again thank you

ballardw
Super User

"Greater than or less than" MAY be treatable as "Not Equal" perhaps in this situation. Is that the intent?

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 20644 views
  • 3 likes
  • 3 in conversation