Logical Expressions In SQL


/ Published in: SAS
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*Most of us SAS programmers approach SQL as simply a data extraction and table joining tool. Since most of us have used the data step longer than SQL, we tend to leave the logic programming to the data step with its if/then statements. However, SQL does have a way of assigning values conditionally. With the CASE expression you can test and assign values logically.
  2. The basic syntax is:
  3.  
  4. CASE value
  5.   WHEN condition THEN result
  6.   WHEN condition THEN result
  7.   ELSE result
  8. END
  9.  
  10. In the code below I am just assigning a 1 or a 0 to a column/variable named bool_tf.
  11. Using the CASE expression is pretty straightforward and is another great way to use SQL to get more coding done in fewer steps.
  12. */
  13.  
  14. data myData;
  15. input answer $;
  16. datalines;
  17. true
  18. false
  19. true
  20. true
  21. false
  22. false
  23. true
  24. ;
  25. proc sql;
  26. create table a as
  27. select answer,
  28. case substr(answer,1,1)
  29. when 't' then 1
  30. when 'f' then 0
  31. end as bool_tf
  32. from myData;
  33. quit;

URL: http://datasteps.blogspot.com/2007/11/using-logical-expressions-in-sql.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.