BookmarkSubscribeRSS Feed
cj9000
Obsidian | Level 7

Question:

The following SAS program is submitted:

data work.test;

First = 'Ipswich, England';

City_Country = substr(First,1,7)!!', '!!'England';

run;

Which one of the following is the length of the variable CITY_COUNTRY in the output data set?

How is this variable length 25; I counted 17 and the length= statement gave 16 which i understand but not the 25.

Thanks

Response:

25

7

6

17

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, you agree that First is 16 characters yes, so how many characters is ", England".  Add the two together and you get a maximum length which is then assigned to the new variable.  Because there is no length specified, SAS is cautious and applies maximum lengths needed.

Astounding
PROC Star

To explain a bit more ...

SAS has to decide on the lengths of variables before reading any data.

When SAS sees the SUBSTR function, it does not examine the third parameter.  In fact, the third parameter (the number of characters to extract) might be an expression that changes from observation to observation.  So to be safe, SAS concludes that it might be taking ALL the characters from FIRST, and might need 16 characters to hold the output of SUBSTR.

So 16 for the output of SUBTR, plus 9 characters hard-coded after that = 25.

cj9000
Obsidian | Level 7

Thanks everybody, I understand now.

Ksharp
Super User

Very interesting question . It appeared that SAS take substr(First,1,7) has the same length as FIRST(16) , therefore you got 25 ,not 17 .

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1921 views
  • 8 likes
  • 4 in conversation