DATA Step, Macro, Functions and more

Length of string variable with 11 characters/digits = 13?

Accepted Solution Solved
Reply
Contributor SM1
Contributor
Posts: 51
Accepted Solution

Length of string variable with 11 characters/digits = 13?

I am working with a string variable (DELIV) that should have a maximum of 11 characters or digits. To prepare this variable, I used the COMPRESS function to remove all spaces.

 

To check and then clean this variable, I ran the following syntax:

 

data checklength;

set check;

    length_deliv = length(deliv);
    lengthn_deliv = lengthn(deliv);
    lengthc_deliv = lengthc(deliv);
run;

 

Here are the results from the SAS Output

length_deliv Frequency Percent Cumulative
Frequency Cumulative
Percent 2 10 11 13 33
10.2110.21
20.4230.63
46397.2746697.90
91.8947599.79
10.21476100.00

lengthn_deliv Frequency Percent Cumulative
Frequency Cumulative
Percent 2 10 11 13 33
10.2110.21
20.4230.63
46397.2746697.90
91.8947599.79
10.21476100.00

lengthc_deliv Frequency Percent Cumulative
Frequency Cumulative
Percent 255
476100.00476100.00

 

When I ran a proc print of the 9 observations where length_deliv and lengthn_deliv = 13,  this is what the values looked like:

 

091705JUN07

262350JUL07

122245OCT07

261635OCT07

152145NOV07

181845NOV07

281820FEB08

021727APR08

090015APR08

 

Why does length_deliv and lengthn_deliv = 13 and not 11?

 

Any help will be most appreciated!


Accepted Solutions
Solution
‎08-23-2017 07:24 PM
Super User
Posts: 22,874

Re: Length of string variable with 11 characters/digits = 13?

Use the 's' modifier with the COMPRESS() function to remove them as well and have no trailing invisible spaces.

 

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#n0fcshr0ir3...

 

Example 3 covers this in the doc.

View solution in original post


All Replies
Super User
Posts: 22,874

Re: Length of string variable with 11 characters/digits = 13?

What are the formats and types associated with the variable?

Contributor SM1
Contributor
Posts: 51

Re: Length of string variable with 11 characters/digits = 13?

I ran a PROC CONTENTS statement to get this info.

 

Type = Char

Len = 255

Format = $255

Informat = $255

Super User
Posts: 22,874

Re: Length of string variable with 11 characters/digits = 13?

Check the values iwth a HEX format, you may have tabs or something else in there. 

 

Or apply the compress function first to remove any non printable characters - you need to use a modifier and recheck the data.

Contributor SM1
Contributor
Posts: 51

Re: Length of string variable with 11 characters/digits = 13?

I compressed the variable to remove spaces before checking the lengths.

 

I have several similar variables and none of the others have any values that are gt 11.

 

I'll figure out how to check the values with a HEX format.

 

Thanks!

Esteemed Advisor
Posts: 5,403

Re: Length of string variable with 11 characters/digits = 13?

Try printing the Lengthn = 13 values with format $hex26. format to see the non printing characters.

PG
Contributor SM1
Contributor
Posts: 51

Re: Length of string variable with 11 characters/digits = 13?

@PG

 

I ran this syntax:

 

proc print data = checklength;
    where lengthn_deliv eq 13;
    format deliv $hex26.;
run;

 

I don't know how to read the SAS output for the 9 values of DELIV where lengthn = 13:

 

30393030313541505230380D0A
30323137323741505230380D0A
32383138323046454230380D0A
3138313834354E4F5630370D0A
3135323134354E4F5630370D0A
3236313633354F435430370D0A
3132323234354F435430370D0A
3236323335304A554C30370D0A

3039313730354A554E30370D0A

Esteemed Advisor
Posts: 5,403

Re: Length of string variable with 11 characters/digits = 13?

0D0A is CR-LF, carriage return - linefeed. So those are the invisible characters that are part of your mysterious values.

PG
Solution
‎08-23-2017 07:24 PM
Super User
Posts: 22,874

Re: Length of string variable with 11 characters/digits = 13?

Use the 's' modifier with the COMPRESS() function to remove them as well and have no trailing invisible spaces.

 

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#n0fcshr0ir3...

 

Example 3 covers this in the doc.

Contributor SM1
Contributor
Posts: 51

Re: Length of string variable with 11 characters/digits = 13?

@Reeza

 

Thank you!

 

I used this syntax to compress a number of similar variables:

 

data want; *compress values to get rid of all spaces;
    set have;
        array change(9)  Inc Alert Launch OnSce Locate Recov Deliv RTB SubmitDate;
                do i = 1 to 9;
                change(i)=compress(change(i), , 's');
    end;
run;

 

It worked perfectly.

 

Thank you!

Contributor SM1
Contributor
Posts: 51

Re: Length of string variable with 11 characters/digits = 13?

@PGStats

 

Indeed, there were invisible characters.

 

Thanks for explaining 0D0A .

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 191 views
  • 0 likes
  • 3 in conversation