BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS47
Calcite | Level 5

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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.


Alphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 15

 

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.

From https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lefunctionsref/p0jshdjy2z9zdzn1h7k90u99lyq6.h...

"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.

View solution in original post

2 REPLIES 2
ballardw
Super User

@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.


Alphabetic List of Variables and Attributes
# Variable Type Len
1 Author Char 15
2 First Char 15

 

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.

From https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lefunctionsref/p0jshdjy2z9zdzn1h7k90u99lyq6.h...

"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.

Astounding
PROC Star

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 2 replies
  • 674 views
  • 3 likes
  • 3 in conversation