BookmarkSubscribeRSS Feed
JatinRai
Obsidian | Level 7
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?
7 REPLIES 7
Patrick
Opal | Level 21
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
JatinRai
Obsidian | Level 7
HI Patrick

If I go by your explanation then why is 'a' not equal to 16?
chang_y_chung_hotmail_com
Obsidian | Level 7
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Patrick
Opal | Level 21
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
Ksharp
Super User
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
art297
Opal | Level 21
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

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
  • 7 replies
  • 1271 views
  • 0 likes
  • 6 in conversation