DATA Step, Macro, Functions and more

Length of a variable which is getting assigned using Scan function

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Length of a variable which is getting assigned using Scan function

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.


Accepted Solutions
Solution
‎05-05-2015 08:29 AM
Respected Advisor
Posts: 3,799

Re: Length of a variable which is getting assigned using Scan function

Posted in reply to NikhilKhurana

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


All Replies
Solution
‎05-05-2015 08:29 AM
Respected Advisor
Posts: 3,799

Re: Length of a variable which is getting assigned using Scan function

Posted in reply to NikhilKhurana

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.

New Contributor
Posts: 2

Re: Length of a variable which is getting assigned using Scan function

Posted in reply to data_null__

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

Thanks for pointing that out. Smiley Happy

Super User
Super User
Posts: 7,970

Re: Length of a variable which is getting assigned using Scan function

Posted in reply to NikhilKhurana

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 190 views
  • 0 likes
  • 3 in conversation