DATA Step, Macro, Functions and more

Length of a character variable

Reply
Contributor
Posts: 24

Length of a character variable

Hi

I submitted the following code:

data _null_;
first= 'ipswich, england';
city=substr(first,1,7)||', '||'England';
l=lengthc(city);
a=lengthc(substr(first,1,7));
put l= a=;
run;

O/P is l=25 a=7.

Can someone explain why is l=25? What I understand is that it equals 16 bytes of first + 2 bytes for space & comma + 7 bytes for england. But why does this happen?

And second question is that why is a not equal to 16?
Respected Advisor
Posts: 4,173

Re: Length of a character variable

Hi

It seems that SAS uses the length of variable "first" and not of the substring to create the new variable "city" (length: 16 +2 + 7 = 25).

HTH
Patrick
Contributor
Posts: 24

Re: Length of a character variable

HI Patrick

If I go by your explanation then why is 'a' not equal to 16?
Regular Contributor
Posts: 241

Re: Length of a character variable

Jatin Rai wrote:

> If I go by your explanation then why is 'a' not equal to 16?



Good question! Who knows. SUBSTR() and LENGTHC() may work differently. SAS, as any languages, isn't completely regular, and has its share of quarks.


It is a good practice to declare the length of a character variable before you use it and not to let sas guess.
Super Contributor
Super Contributor
Posts: 3,174

Re: Length of a character variable

Posted in reply to chang_y_chung_hotmail_com
SAS does have expected behavior when dealing with CHARACTER type variable assignments - pretty well documented and mostly consistent.

Suggest if you want to ensure an expected SAS CHARACTER variable length to avoid truncation, use an ATTRIB or LENGTH statement (and not a FORMAT statement, by the way). And you will want to code the statement ahead of the first reference to a given SAS CHARACTER variable, that being either with an assignment stmt, a SET, or a RETAIN, to cover most instances.

Scott Barry
SBBWorks, Inc.
Respected Advisor
Posts: 4,173

Re: Length of a character variable

Hi Jatin

We SAS users are just too spoiled by SAS doing so much for us implicit.

So may be SAS R&D decided here to take the safe way and assign a variable length which is always long enough - instead of trying to cater for all the possibilities of how people might create a concatenated string.

I.e: using cat() for concatenation you even end up with the default length for character variables ($200 in my environment).

As others already said: If you want to be sure what you get define it explicit using a length statement.

HTH
Patrick
Super User
Posts: 10,028

Re: Length of a character variable

Hi.
All make me some sense is 'substr(first,1,7)' is only in PDV ( not write into disk) so SAS will treat it has the same storage length with variable 'First' . and once you write it into dataset (such as ' a=substr(first,1,7) '),it will has 7 length of storage.


Ksharp
PROC Star
Posts: 7,471

Re: Length of a character variable

Jatin,

I don't know if the details regarding assigning variable length in such a condition are documented but, if you want the calculation to equal 16, just use a function that doesn't include trailing blanks. I.e., use length rather than lengthn.

HTH,
Art
Ask a Question
Discussion stats
  • 7 replies
  • 431 views
  • 0 likes
  • 6 in conversation