Hello Everyone,
I am preparing for BASE SAS Specialist. A very common question in Dumps is to find the length of First Variable. I have coded in this SAS studio (Please Find the Code Below) and the length of the variable First is 200 but I am not able to find the reason for this.
Please Let me know why length of the variable first (in below code) would be 200?
data work.test;
Author= 'Agatha Christie';
First=substr(scan(author,1, ' '),1,1);
run;
@SAS47 wrote:
Hello Everyone,
I am preparing for BASE SAS Specialist. A very common question in Dumps is to find the length of First Variable. I have coded in this SAS studio (Please Find the Code Below) and the length of the variable First is 200 but I am not able to find the reason for this.
Please Let me know why length of the variable first (in below code) would be 200?
data work.test;
Author= 'Agatha Christie';
First=substr(scan(author,1, ' '),1,1);
run;
Suggestion: Run that code and examine the result. Proc Contents is your friend when dealing with specific examples. Run the data step then contents and there's the answer.
It appears that perhaps your SAS studio is not following the latest documentation (or the results from SAS 9.4) as I get 15 not 200.
The documentation for most, if not all, of the character manipulation functions will have a bit on what the default lengths are, and how they are assigned.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lefunctionsref/n0n08xougp40i5n1xw7njpcy0a2b.h...
for example.
Which says the variable would have the length of the first parameter.
So you look at SCAN to see how it sets length as the first parameter to SUBSTR is the result of SCAN function.
Which will use the length of it's first parameter, or that of Author.
"In a DATA step, if the SCAN function returns a value to a variable that has not yet been given a length, that variable is given the length of the first argument."
There is a note about older versions of SAS set lengths of 200 for SCAN result, so be careful with older references. So perhaps your SAS Studio is connecting to something not running a recent release of SAS.
Don't know what "Dumps" is so can't comment on anything there.
@SAS47 wrote:
Hello Everyone,
I am preparing for BASE SAS Specialist. A very common question in Dumps is to find the length of First Variable. I have coded in this SAS studio (Please Find the Code Below) and the length of the variable First is 200 but I am not able to find the reason for this.
Please Let me know why length of the variable first (in below code) would be 200?
data work.test;
Author= 'Agatha Christie';
First=substr(scan(author,1, ' '),1,1);
run;
Suggestion: Run that code and examine the result. Proc Contents is your friend when dealing with specific examples. Run the data step then contents and there's the answer.
It appears that perhaps your SAS studio is not following the latest documentation (or the results from SAS 9.4) as I get 15 not 200.
The documentation for most, if not all, of the character manipulation functions will have a bit on what the default lengths are, and how they are assigned.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lefunctionsref/n0n08xougp40i5n1xw7njpcy0a2b.h...
for example.
Which says the variable would have the length of the first parameter.
So you look at SCAN to see how it sets length as the first parameter to SUBSTR is the result of SCAN function.
Which will use the length of it's first parameter, or that of Author.
"In a DATA step, if the SCAN function returns a value to a variable that has not yet been given a length, that variable is given the length of the first argument."
There is a note about older versions of SAS set lengths of 200 for SCAN result, so be careful with older references. So perhaps your SAS Studio is connecting to something not running a recent release of SAS.
Don't know what "Dumps" is so can't comment on anything there.
So you have seen what results from the code, but why?
When creating a new variable, SAS has to decide what length to use. Why doesn't it use a length of 1, which is the third parameter to SUBSTR? The answer is that the third parameter does not have to be hard-coded. It could be an expression based on numeric variables on the data set (and could therefore vary from one observation to the next). So SAS doesn't even examine the third parameter to decide what length to use. To be safe, SAS decides to assign the length of the first parameter. After all, it might need that length if SUBSTR happens to select the entire string.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.