DATA Step, Macro, Functions and more

SAS select function

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

SAS select function

 

data temp;

test = 'X';

select(test);

when('Y') Name = 'Ste';

when('X') Name = 'Steve';

when('Z') Name = ' ';

end;

run;

What is the output?

  • A) Steve;
  • B) Ste
  • C) Missing (character missing value)
  • D) SYNTAX ERROR

The answer is B which I do not understand, Isn't only X fulfiled the criteria so it should output "Steve"? Thanks so much for helping!


Accepted Solutions
Solution
‎09-24-2017 12:00 AM
Respected Advisor
Posts: 4,173

Re: SAS select function

[ Edited ]

@pchen002

That's a "mean" question - but a good one.

 

You are absolutely correct that the 2nd condition when('X') Name = 'Steve'; is the one which gets TRUE

 

If you actually execute the data step you've posted you'll still see that in the result table temp variable Name will have a value of Ste.

 

The reason for this is that the first time you're using variable Name in your data step it gets a string with 3 characters assigned.

Think of each data step having an iteration zero where the SAS compiler investigates the data step and creates all necessary variables. It's with the SAS compiler first comes first and for Name it finds a string assignment with 3 characters and though creates the variable Name only with a length of 3 characters.

When SAS then iterates through the observations (iteration one to n) then your 2nd condition becomes TRUE and SAS assigns Steve to Name. ...but as variable Name has only a length of 3 the string gets truncated and you end up with Ste.

 

What this sample teaches you: Define variable lengths for such cases explicitly at the beginning of your data step.

data temp;
length test $1 name $5;
test = 'X';
select(test);
when('Y') Name = 'Ste';
when('X') Name = 'Steve';
when('Z') Name = ' ';
end;
run;

 

Update: Code fixed as per @PGStats post.

 

 

 

View solution in original post


All Replies
Solution
‎09-24-2017 12:00 AM
Respected Advisor
Posts: 4,173

Re: SAS select function

[ Edited ]

@pchen002

That's a "mean" question - but a good one.

 

You are absolutely correct that the 2nd condition when('X') Name = 'Steve'; is the one which gets TRUE

 

If you actually execute the data step you've posted you'll still see that in the result table temp variable Name will have a value of Ste.

 

The reason for this is that the first time you're using variable Name in your data step it gets a string with 3 characters assigned.

Think of each data step having an iteration zero where the SAS compiler investigates the data step and creates all necessary variables. It's with the SAS compiler first comes first and for Name it finds a string assignment with 3 characters and though creates the variable Name only with a length of 3 characters.

When SAS then iterates through the observations (iteration one to n) then your 2nd condition becomes TRUE and SAS assigns Steve to Name. ...but as variable Name has only a length of 3 the string gets truncated and you end up with Ste.

 

What this sample teaches you: Define variable lengths for such cases explicitly at the beginning of your data step.

data temp;
length test $1 name $5;
test = 'X';
select(test);
when('Y') Name = 'Ste';
when('X') Name = 'Steve';
when('Z') Name = ' ';
end;
run;

 

Update: Code fixed as per @PGStats post.

 

 

 

Respected Advisor
Posts: 4,930

Re: SAS select function

@Patrick, you meant

 

length test $1 name $5;

PG
Contributor
Posts: 31

Re: SAS select function

Ya, I do not quite understand too, how do you know " variable Name in your data step it gets a string with 3 characters assigned"?? Please guide, thanks!

Respected Advisor
Posts: 4,173

Re: SAS select function

[ Edited ]

@pchen002

The variable NAME in your data step gets created with a length of 3 so it can only hold 3 characters. That's too short for Steve (=5 characters). The string will get truncated to 3 characters (Ste).

 

How the SAS data step compilation phase works and how the PDV gets created are important basics for you to learn and understand.

The SAS Language Reference: Concepts manual covers a lot of these topics and it's documentation you should read and try to understand.

https://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p08a4x7h9mkwqvn16jg...

 

Super User
Super User
Posts: 7,074

Re: SAS select function


pchen002 wrote:

Ya, I do not quite understand too, how do you know " variable Name in your data step it gets a string with 3 characters assigned"?? Please guide, thanks!


Generally SAS will define a variable when it first encounters when compiling the data step. In you example since there are no input datesets that could ahve contained the NAME variable the first place that NAME is mentioned is in this assignment statement.

Name = 'Ste';

Since you are setting NAME to a string constant SAS will define NAME as a character with the length of the constant that you are setting it to.

Depending on what order you placed your three WHEN statements you could have made NAME as length $5 (like below) or length $1.

data temp;
test = 'X';
select(test);
when('X') Name = 'Steve';
when('Y') Name = 'Ste';
when('Z') Name = ' ';
end;
run;
Contributor
Posts: 31

Re: SAS select function

Thanks!!Understood!

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 248 views
  • 2 likes
  • 4 in conversation