Help using Base SAS procedures

Using select statement

Accepted Solution Solved
Reply
Occasional Contributor bgs
Occasional Contributor
Posts: 15
Accepted Solution

Using select statement

Code as written by me: Approach1

data temp_sw;

set ramu.temp;

select;

when (Expense>4000) spendanalysed="Low class";

and when (Expense>20000) spendanalysed="Middle class";

and when (Expense>50000) spendanalysed="Affluent";

otherwise spendanalysed="Poor";

end;

run;

 

Code as written by me: Approach2

data temp_sw;

set ramu.temp;

length spendanalysed $25;

select;

if (Expense>4000) spendanalysed="Low class";

else if (Expense>20000) spendanalysed="Middle class";

else if (Expense>50000) spendanalysed="Affluent";

else do spendanalysed="Poor";

end;

run;

 

In both aprroaches, i am getting an error in the log. the respective logs are attached below for your reference.

 

Please help:-(


Accepted Solutions
Solution
‎01-17-2016 05:45 AM
Respected Advisor
Posts: 4,920

Re: Using select statement

As others have said: respect the syntax. If it is not described in the syntax definition, it won't work.

Also, the conditions in s select block are tested sequentially until one of them is true; other conditions after that one are not tested. So you must test the highest range first because if Expense is greater than 50000 then it is also greater than 4000. If you test it against 4000 first, you will never test it against 50000.

 

data temp_sw;
set ramu.temp;
length spendanalysed $25;
select;
	when (Expense>50000) spendanalysed="Affluent";
	when (Expense>20000) spendanalysed="Middle class";
	when (Expense>4000) spendanalysed="Low class";
	otherwise spendanalysed="Poor";
	end;
run;

 

 

PG

View solution in original post


All Replies
Occasional Contributor bgs
Occasional Contributor
Posts: 15

Re: Using select statement

Attachements of Logs


Approach1 - log.PNGApproach2 - log.PNG
Solution
‎01-17-2016 05:45 AM
Respected Advisor
Posts: 4,920

Re: Using select statement

As others have said: respect the syntax. If it is not described in the syntax definition, it won't work.

Also, the conditions in s select block are tested sequentially until one of them is true; other conditions after that one are not tested. So you must test the highest range first because if Expense is greater than 50000 then it is also greater than 4000. If you test it against 4000 first, you will never test it against 50000.

 

data temp_sw;
set ramu.temp;
length spendanalysed $25;
select;
	when (Expense>50000) spendanalysed="Affluent";
	when (Expense>20000) spendanalysed="Middle class";
	when (Expense>4000) spendanalysed="Low class";
	otherwise spendanalysed="Poor";
	end;
run;

 

 

PG
Occasional Contributor bgs
Occasional Contributor
Posts: 15

Re: Using select statement

@PGStats and @FreelanceReinhard - Thank you. I can understand this now.

 

Another way to write the code for the same outcome is I believe:

 

data temp_sw;
set ramu.temp;
length spendanalysed $25;
select(Expense); /*here you can select the variable and then define the conditions*/
when(>50000) spendanalysed="Affluent";
when(>20000) spendanalysed="Middle class";
when(>4000) spendanalysed="Low class";
otherwise spendanalysed="Poor";
end;
run;

 

Could you also correct the syntax here - I am getting an error in the log

Or is it that I cannot use this form of entry for tjhe desired output?

Trusted Advisor
Posts: 1,117

Re: Using select statement

Please have a look at the documentation of the SELECT statement. There, in the short paragraph titled "Evaluating the when-expression When a select-expression Is Included" (which applies to the situation that you write select(something); rather than select;), it says: "SAS evaluates the select-expression and when-expression. SAS compares the two for equality and returns a value of true or false."

 

Your when-expressions (e.g. ">50000"), however, cannot be evaluated (to a number or character string). They are not even valid SAS expressions, because they are incomplete. In particular, they cannot be equal to any of the numeric values of Expense (the select-expression).

 

So, if Expense was your select-expression, you would have to list all possible values (at least those present in the data) in the respective categories of the WHEN expressions, e.g. WHEN(4000.01, 4000.02, 4000.03,

<many pages later and assuming that your Expense values don't have more than two decimals>

19999.97, 19999.98, 19999.99, 20000) spendanalysed="Low class";

 

You see: This is infeasible with your intended WHEN conditions. Hence, stick to PG's suggestion.

 

One more hint: Please note that missing values of Expense would be classified as "Poor" in variable spendanalysed, because they don't satisfy any of the three WHEN conditions. To avoid this, you could add a fourth WHEN condition and statement, e.g.

when (missing(Expense)) spendanalysed="Missing";

 

 

 

Trusted Advisor
Posts: 1,117

Re: Using select statement

Simply stick to the syntax of the SELECT statement and do not write ANDs before WHEN, let alone replace WHEN by IF/ELSE IF.

Super User
Posts: 19,789

Re: Using select statement

If you did want to use IF/ELSE remember the THEN. Its always helpful to check against the doc as well.

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202239.htm
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 431 views
  • 1 like
  • 4 in conversation