Hi,
I'm a new user and I started reading on using select-when statements with a data step. I read the documentation (link: Select/When Documentation) and attempted to run (copy & paste) the examples SAS Help provides into SAS studio but none of them ran successfully.
(1) The examples provided are missing the DATA step, is this why they do not work?
This has happened before in other documentation examples and (2) I just wonder WHY documentation does not have example code that actually executes?? Why provide incomplete examples?!...I am using SAS Studio, which assigns specific colors to the code formatting (?), so that was my first clue that the examples would not work as is...anywhoo, any help would greatly appreciated.
Thanks,
Dominique
/*data months; set work.months;*/ *is this what I am missing?...
Or something similar, bc this doesn't work either; *code below was provided in SAS Help Documentation; *
Example 1;
select (a); when (1) x=x*10; when (2); when (3,4,5) x=x*100; otherwise; end; *
Example 2;
select (payclass); when ('monthly') amt=salary; when ('hourly') do; amt=hrlywage*min(hrs,40); if hrs>40 then put 'CHECK TIMECARD'; end; /* end of do */ otherwise put 'PROBLEM OBSERVATION'; end; /* end of select */
*
Example 3;
select;
when (mon in ('JUN', 'JUL', 'AUG')
and temp>70) put 'SUMMER ' mon=;
when (mon in ('MAR', 'APR', 'MAY'))
put 'SPRING ' mon=;
otherwise put 'FALL OR WINTER ' mon=;
end;
My Log statement produces 19 ERROR 180-322: Statement is not valid or it is used out of proper order.
You are right. Many examples are given without the context that would make them cut-and-paste executable. You must infer the context.
Is there some aspect of the SELECT statement that puzzles you?
Hi,
You are absolutely right, I think I posted my question more so out of frustration with the documentation examples I've run into rather than anything specifically related to select/when statements. In terms of context, if I had a data set with the variable 'selected' in the examples (ie 'payclass') the code would run just fine.
*
Thanks all for replying! I misunderstood how to use the documentation examples, but I get it now. Sometimes too much information can cause just as much confusion and sas documentation wants to keep the references relevant to specific topics.
Thanks again!
The code samples are just fragments to show the syntax, not complete programs.
This works:
I get your point but when complete data sets and worked examples are provided they also confuse users because then there's extraneous information and they don't know which is actually relevant. There are thousands of examples of that on here where someone posts a solution and then a user won't understand how to apply it to their data because it's a full solution.
Not really sure I've seen a full answer to this issue in documentation besides full training courses. My assumption, is that you essentially have to learn to read the documentation as well - for every language you learn.
For example, I'm not a fan of R documentation because you can only see the output when you actually run the code and sometimes it won't do that either.
@domi_versaix wrote:
Hi,
I'm a new user and I started reading on using select-when statements with a data step. I read the documentation (link: Select/When Documentation) and attempted to run (copy & paste) the examples SAS Help provides into SAS studio but none of them ran successfully.
(1) The examples provided are missing the DATA step, is this why they do not work?
This has happened before in other documentation examples and (2) I just wonder WHY documentation does not have example code that actually executes?? Why provide incomplete examples?!...I am using SAS Studio, which assigns specific colors to the code formatting (?), so that was my first clue that the examples would not work as is...anywhoo, any help would greatly appreciated.
Thanks,
Dominique
/*data months; set work.months;*/ *is this what I am missing?...
Or something similar, bc this doesn't work either; *code below was provided in SAS Help Documentation; *
Example 1;
select (a); when (1) x=x*10; when (2); when (3,4,5) x=x*100; otherwise; end; *
Example 2;
select (payclass); when ('monthly') amt=salary; when ('hourly') do; amt=hrlywage*min(hrs,40); if hrs>40 then put 'CHECK TIMECARD'; end; /* end of do */ otherwise put 'PROBLEM OBSERVATION'; end; /* end of select */
*
Example 3;select;
when (mon in ('JUN', 'JUL', 'AUG')
and temp>70) put 'SUMMER ' mon=;
when (mon in ('MAR', 'APR', 'MAY'))
put 'SPRING ' mon=;
otherwise put 'FALL OR WINTER ' mon=;
end;My Log statement produces 19 ERROR 180-322: Statement is not valid or it is used out of proper order.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.