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

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.

 

 

 

 

 

 

https://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=p09213s9jc2t99n1vx0omk2rh9ps.htm&doc...

1 ACCEPTED SOLUTION

Accepted Solutions
ebowen
Quartz | Level 8
Yes, you're right. Select/When statements are meant to be part of a data step. The Select (a); refers to the "a" variable in the dataset from the set statement in the data step. When(1) means to take some action when the value of "a" equals 1. Hope that helps!

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

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?

PG
domi_versaix
Calcite | Level 5

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!

tomrvincent
Rhodochrosite | Level 12

The code samples are just fragments to show the syntax, not complete programs.

 

This works:

 

data null;
h1=hour(datetime());
select;
when  (h1<12) greetp1 = "Morning  "
;
otherwise greetp1='foo';
end;
 
put greetp1;
run;
ebowen
Quartz | Level 8
Yes, you're right. Select/When statements are meant to be part of a data step. The Select (a); refers to the "a" variable in the dataset from the set statement in the data step. When(1) means to take some action when the value of "a" equals 1. Hope that helps!
Reeza
Super User

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.

 

 

 

 

 

 

https://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=p09213s9jc2t99n1vx0omk2rh9ps.htm&doc...


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 1192 views
  • 4 likes
  • 5 in conversation