data temp;
test = 'X';
select(test);
when('Y') Name = 'Ste';
when('X') Name = 'Steve';
when('Z') Name = ' ';
end;
run;
What is the output?
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!
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.
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.
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!
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.
@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;
Thanks!!Understood!
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.
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.