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

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:-(

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

6 REPLIES 6
bgs
Obsidian | Level 7 bgs
Obsidian | Level 7

Attachements of Logs


Approach1 - log.PNGApproach2 - log.PNG
PGStats
Opal | Level 21

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
bgs
Obsidian | Level 7 bgs
Obsidian | Level 7

@PGStats and @FreelanceReinh - 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?

FreelanceReinh
Jade | Level 19

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

 

 

 

FreelanceReinh
Jade | Level 19

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

Reeza
Super User
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

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
  • 6 replies
  • 1542 views
  • 1 like
  • 4 in conversation