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:-(
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;
Attachements of Logs
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;
@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?
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";
Simply stick to the syntax of the SELECT statement and do not write ANDs before WHEN, let alone replace WHEN by IF/ELSE IF.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.