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

 

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@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

6 REPLIES 6
Patrick
Opal | Level 21

@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.

 

 

 

PGStats
Opal | Level 21

@Patrick, you meant

 

length test $1 name $5;

PG
pchen002
Obsidian | Level 7

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!

Patrick
Opal | Level 21

@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...

 

Tom
Super User Tom
Super User

@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;
pchen002
Obsidian | Level 7

Thanks!!Understood!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1264 views
  • 2 likes
  • 4 in conversation