BookmarkSubscribeRSS Feed
cons
Calcite | Level 5

Hi everybody,

 

my name is Constantin. I need help with the subject above. 

I dont get it. The manual says

"In a DATA step, if the SCAN function returns a value to a variable that has not yet been given a length, then that variable is given a length of 200 characters. If you need the SCAN function to assign to a variable a word that is longer than 200 characters, then you should explicitly specify the length of that variable".

 

Ok. I did try it, but it didnt work.

 

Example:

DATA data;

name=scan('Mr Smith',2);

RUN;

 

I get the length 8. Please, help me.

 

Bye.

5 REPLIES 5
Astounding
PROC Star

The manual is wrong here.

 

There are many functions that return character strings, and for many of those the manual would be correct.  (For many additional character functions, the new variable would be assigned a length equal to the length of the incoming string.)  But SCAN returns a number, not a character string.  The default length for numbers is 8.

data_null__
Jade | Level 19

Are you sure about that?

 


@Astounding wrote:

The manual is wrong here.

 

There are many functions that return character strings, and for many of those the manual would be correct.  (For many additional character functions, the new variable would be assigned a length equal to the length of the incoming string.)  But SCAN returns a number, not a character string.  The default length for numbers is 8.


 

Astounding
PROC Star

No, my mistake.  I was mixing together two functions.  SCAN definitely returns a character string, not a number.

 

If you think about the result, this behavior makes sense.  SAS has to decide on a length for the new variable before it actually examines the data.  It is possible that SCAN will retrieve the entire incoming string, so to be safe it assigns the new variable the length of the incoming string.  Same behavior for SUBSTR.

FreelanceReinh
Jade | Level 19

Hi @cons,

 

The SAS 9.4 documentation of the SCAN function says:

"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. This behavior is different from the behavior in previous releases of SAS. In previous releases, code that created a variable with a length of 200 might have produced a variable with a length that was greater than expected."

 

So, I guess, you're using SAS 9.4, but you read the documentation of an earlier SAS version. The string "Mr Smith" has a length of 8 characters, hence the length of variable NAME.

cons
Calcite | Level 5
Thank u great job FreelanceReinhard

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 3949 views
  • 2 likes
  • 4 in conversation