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

Here's a SAS BASE certification exam sample question:

The following SAS program is submitted:

data work.flights;

     destination = 'CPH';

     select(destination);

          when('LHR') city = 'London';

          when('CPH') city ='Copenhagen';

          otherwise;

     end;

run;

Which of the following is the value of the CITY variable?

A. London

B. Copenh

C. Copenhagen

D. ' ' (missing char value)

why is the answer B not C? also, within the Select statement, what is the function of 'otherwise'?

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Davidliu494
Calcite | Level 5

nvm... it's because variable CITY took the length of the first instance of the variable, which is 'London'

View solution in original post

3 REPLIES 3
Davidliu494
Calcite | Level 5

nvm... it's because variable CITY took the length of the first instance of the variable, which is 'London'

Linlin
Lapis Lazuli | Level 10

The length of new variable "city" was set by "London".  using "otherwise" because destination may have values other than "LHR" and "CPH".

try adding a length statement:

data work.flights;

  length city $ 10;

     destination = 'CPH';

     select(destination);

          when('LHR') city = 'London';

          when('CPH') city ='Copenhagen';

          otherwise;

     end;

run;

Tom
Super User Tom
Super User

Otherwise is where it branches when none of the other conditions are met.  In this case it does nothing.  You can cause run-time errors if you forget to include the otherwise category and your actual data fails to match one of the other conditions.  Example:

ERROR: Unsatisfied WHEN clause and no OTHERWISE clause at line 110 column 3.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2037 views
  • 7 likes
  • 3 in conversation