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

Hi,

I was reading Scan function on support site (SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition) . It is written that, 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.

But when I am executing the below data steps

data test1;

title = 'Writing as much Macros as possible so that they can be used in other modules as well.';

word=scan(title,1,' ');

run;

data test2;

word=scan('Writing as much Macros as possible so that they can be used in other modules as well.',1,' ');

run;

The length of variable word in both the data sets is 85 .

I just need to understand why is the length not 200 and 85.

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

You need to look at the documentation for the version of SAS that you are actually using.

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. If you need the SCAN function to assign to a variable a value that is different from the length of the first argument, use a LENGTH statement for that variable before the statement that uses the SCAN function.

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

You need to look at the documentation for the version of SAS that you are actually using.

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. If you need the SCAN function to assign to a variable a value that is different from the length of the first argument, use a LENGTH statement for that variable before the statement that uses the SCAN function.

NikhilKhurana
Calcite | Level 5

My bad, I am using SAS 9.4 but was looking at SAS 9.2 documentation.

Thanks for pointing that out. Smiley Happy

RW9
Diamond | Level 26 RW9
Diamond | Level 26

"you should explicitly specify the length of that variable"

IMO you should always explicitly state the length of a variable.  Otherwise you may get differing results if the data changes.

sas-innovate-2024.png

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.

 

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
  • 3 replies
  • 742 views
  • 0 likes
  • 3 in conversation