DATA Step, Macro, Functions and more

scan function -need help

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

scan function -need help

Hi! 

I have a question. Which is the different between this picture?

I don.t understand when the lenght is 200 bytes or the lenght of variable

 

 

The following SAS program is submitted:  
data work.test;
   title="Hitchhiker's Guide to SAS's Functions";
   word=scan(title,5," ,':");
run;
What is the value and length of the variable Word in the output data set?

 

a.The value is Functions and the length is 9 bytes.
b.The value is Functions and the length is 37 bytes.
c.The value is SAS and the length is 37 bytes.
d.The value is SAS and the length is 200 bytes.

Correct answer:  c

  Your answer:  d
  
As of SAS 9.4, 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. In this case, the length of the variable word is 37, the same as the variable title.Return to top

 

 

 

The following SAS program is submitted:

data work.words;
   word1='Is';
   word2='It';
   all=catx(' ',word1,word2);
run;
The variable All is a character variable of what size?

 

a.200 -- standard default size
b.4 -- all of the characters added together
c.5 -- all of the characters and 1 space
d.8 -- character default size of 8

Correct answer:   a

   
   

 

Thanks!!!!!!!!!


Accepted Solutions
Solution
‎11-14-2016 02:58 AM
SAS Super FREQ
Posts: 8,740

Re: scan function -need help

Hi:
In the "old" days, SCAN did return a length of $200 when you created a new variable using SCAN. But, as the note indicates in the explanation for the right answer, starting with SAS 9.4, if you do not explicitly specify a length, then SCAN uses the length of the first argument to the function (instead of $200) -- on the theory that if you are extracting a piece of the first argument, the returned size can't be bigger than the length of the first argument.

Just because CAT,CATX, CATT functions work with creating one length, you can't assume that all character functions work the same.

A good rule of thumb is to always use a LENGTH statement when creating new character variables, then you are in control of the LENGTH that will be used instead of the default, whatever it is.

(If you look in the doc for the CAT function, for example, http://support.sas.com/documentation/cdl/en/ds2ref/68052/HTML/default/viewer.htm#n0somj6dsx2dmkn1vqj... you will see that $200 is not always the length used.)

cynthia

View solution in original post


All Replies
Contributor
Posts: 37

Re: scan function -need help

Hey,

Cats, Catx, and Cat func always give 200 bytes len for the resulting var, untill unless you do'nt define the length explicitly,

if you use || to concat then you get len of word 1 + word 2, so in ur case 

if you use

data work.words;
   word1='Is';
   word2='It';
   all=(word1||word2;
run;

proc conetnts;run;

in this case you will find 4 len for all variable.

 

Occasional Contributor
Posts: 14

Re: scan function -need help

I don,t understand you!! because in my first example I have scan I don,t know because the lenght isn,t 200 and the second example yes!
Contributor
Posts: 37

Re: scan function -need help

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.

Contributor
Posts: 37

Re: scan function -need help

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

 

  2-  If you use the SCAN function in an expression that contains operators or other functions, a word that is returned by the SCAN function can have a length of up to 32,767 characters, except in a WHERE clause. In that case, the maximum length is 200 characters.

 

3-  In the SQL procedure, or in a WHERE clause in any procedure, the maximum length of a word that is returned by the SCAN function is 200 characters.

 

4- In the macro processor, the maximum length of a word that is returned by the SCAN function is 65,534 characters.

 

 

Source :

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000214639.htm

 

Occasional Contributor
Posts: 14

Re: scan function -need help

then, because in my first example the solution isn,t 200??
Solution
‎11-14-2016 02:58 AM
SAS Super FREQ
Posts: 8,740

Re: scan function -need help

Hi:
In the "old" days, SCAN did return a length of $200 when you created a new variable using SCAN. But, as the note indicates in the explanation for the right answer, starting with SAS 9.4, if you do not explicitly specify a length, then SCAN uses the length of the first argument to the function (instead of $200) -- on the theory that if you are extracting a piece of the first argument, the returned size can't be bigger than the length of the first argument.

Just because CAT,CATX, CATT functions work with creating one length, you can't assume that all character functions work the same.

A good rule of thumb is to always use a LENGTH statement when creating new character variables, then you are in control of the LENGTH that will be used instead of the default, whatever it is.

(If you look in the doc for the CAT function, for example, http://support.sas.com/documentation/cdl/en/ds2ref/68052/HTML/default/viewer.htm#n0somj6dsx2dmkn1vqj... you will see that $200 is not always the length used.)

cynthia
Occasional Contributor
Posts: 14

Re: scan function -need help

Hi Cyntia,
Then, do SAS 9.4 always have the lenght of the first argument to the function alway ?
SAS Super FREQ
Posts: 8,740

Re: scan function -need help

No -- the length of the new variable really depends on the function you are using. That was why I pasted the link for the CAT function in my previous posting. So, by comparison to SCAN, you could see that CAT was different.

I really recommend a review of the documentation for the character functions that you use most often. As I indicated, it is considered a best practice to use a LENGTH statement so you do not have to count on the defaults.

cynthia
Super User
Posts: 9,670

Re: scan function -need help

I remember @Astounding said before.
SAS will decide the length of variable before applying a function.
Therefore SAS will take SCAN's variable length as response variable length.
But for CATX() , SAS can't decide the variable length ,so using 200 as default length.

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 429 views
  • 0 likes
  • 4 in conversation