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 |
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.
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.
Thanks everybody, I understand now.
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 .
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.