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-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!

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.

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
  • 4 replies
  • 1504 views
  • 8 likes
  • 4 in conversation