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

Pic.jpgHi,

 

I am using VFL 4 SAS Studio. I want to create a new column using a conditional statement (see image below). I cannot write the case when although the software provides guidelines:

CASE <case-operand>
WHEN when-condition
THEN result-expression
<ELSE result-expression>
END

Can you provide an example?

Thanks,

Andreas

azaras_0-1718722343416.pngazaras_1-1718722367526.png

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
azaras
Obsidian | Level 7

When i use thre Query node everythiong is fine.

 

I use 

 

Case (Order_Type)

when 1 then 'X'

when 2 then 'Y'

else 'V'

end

 

Though in the computed columns node it does not work.

 

Any ideas?

 

View solution in original post

5 REPLIES 5
ballardw
Super User

It would be a good idea for you to describe the variables, conditions and needed results.

 

Sometimes the point and click interface is not the best way to get results and CASE when syntax can get extremely cumbersome if there are large numbers of different results and variables involved.

 

case categoryvariable     <= variable to use in all comparisons
   when 'A' then  2.5        <= compare variable to 'A' and set result
   when 'B' then  3.7        <= compare variable to 'B' and set result
   when 'C' then  4.4        <= compare variable to 'C' and set result
   else 1                    <= result for all other values of the variable
end   as multiplier          <= end the Case expression assign result to multiplier
azaras
Obsidian | Level 7

Hi, Thanks for your answer.

 

I get the following into the log  so i guess case is not a valid way for the condiitonal proceccing.

 

ANt ideas?

 

Thnaks

 

 

NOTE: The data set WORK._TMP_0F2A7CAC2D8C11EF87B43A81D86 has 0 observations and 13 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
 
 
138
139 data null;
140 set work._tmp_0f2a7cac2d8c11ef87b43a81d86;
141 __valid_expr=case (Order_Type)
----
68
142 when 1 then 2.5
----
22
76
ERROR 68-185: The function CASE is unknown, or cannot be accessed.
 
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, ;, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT,
IN, LE, LT, MAX, MIN, NE, NG, NL, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.
 
ERROR 76-322: Syntax error, statement will be ignored.

 

azaras
Obsidian | Level 7

When i use thre Query node everythiong is fine.

 

I use 

 

Case (Order_Type)

when 1 then 'X'

when 2 then 'Y'

else 'V'

end

 

Though in the computed columns node it does not work.

 

Any ideas?

 

azaras
Obsidian | Level 7

The computed column node sims to have an issue. I will use the Query in stead that is working properly.

 

Thanks!

ballardw
Super User

CASE /WHEN is SQL syntax, not DATA step.

 

 

Even at that the "new column" comes after the END of the Case construct

Case when <condition> result

end as NEWCOLUMN

 

Data step would use IF/Then or SELECT

If variable = value then newvariable=something;
ELSE if variable= othervalue then Newvariable=somethingelse;
<else ...>

Or

Select (variable);
   when (value) NewVariable=something;
   when (othervalue) Newvariable=somethingelse;
   otherwise <what to do when none of the listed values are encountered>;
end;