05-09-2015 07:58 PM
The following SAS program is submitted:
First = 'Ipswich, England';
City_Country = substr(First,1,7)!!', '!!'England';
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.
05-11-2015 05:38 AM
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.
05-11-2015 08:47 AM
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.
05-11-2015 08:17 AM
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 .