Help using Base SAS procedures

Confused on the length of this variable, thx in advance

Reply
Contributor
Posts: 27

Confused on the length of this variable, thx in advance

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

Super User
Super User
Posts: 7,392

Re: Confused on the length of this variable, thx in advance

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.

Super User
Posts: 5,079

Re: Confused on the length of this variable, thx in advance

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.

Contributor
Posts: 27

Re: Confused on the length of this variable, thx in advance

Thanks everybody, I understand now.

Super User
Posts: 9,671

Re: Confused on the length of this variable, thx in advance

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 .

Ask a Question
Discussion stats
  • 4 replies
  • 309 views
  • 3 likes
  • 4 in conversation