SAS default length

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

SAS default length

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!


Accepted Solutions
Solution
‎10-17-2012 04:26 PM
Occasional Contributor
Posts: 9

Re: SAS default length

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

View solution in original post


All Replies
Solution
‎10-17-2012 04:26 PM
Occasional Contributor
Posts: 9

Re: SAS default length

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

Super Contributor
Posts: 1,636

Re: SAS default length

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;

Super User
Super User
Posts: 6,502

Re: SAS default length

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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