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

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!!!!!!!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ
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

9 REPLIES 9
LittlesasMaster
Obsidian | Level 7

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.

 

beatraca
Fluorite | Level 6
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!
LittlesasMaster
Obsidian | Level 7

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.

LittlesasMaster
Obsidian | Level 7

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

 

beatraca
Fluorite | Level 6
then, because in my first example the solution isn,t 200??
Cynthia_sas
SAS Super FREQ
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
beatraca
Fluorite | Level 6
Hi Cyntia,
Then, do SAS 9.4 always have the lenght of the first argument to the function alway ?
Cynthia_sas
SAS Super FREQ
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
Ksharp
Super User
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 3164 views
  • 0 likes
  • 4 in conversation