turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- If this greater than or less than <>

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-28-2014 05:52 PM

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

Accepted Solutions

Solution

08-29-2014
07:14 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BETO

08-29-2014 07:14 AM

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BETO

08-28-2014 06:04 PM

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"** .

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BETO

08-28-2014 06:57 PM

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 .

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-28-2014 07:11 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BETO

08-28-2014 10:36 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-29-2014 12:31 AM

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

Solution

08-29-2014
07:14 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BETO

08-29-2014 07:14 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-29-2014 03:46 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BETO

08-29-2014 11:20 AM

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