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
1 | 0.21 | 1 | 0.21 |
2 | 0.42 | 3 | 0.63 |
463 | 97.27 | 466 | 97.90 |
9 | 1.89 | 475 | 99.79 |
1 | 0.21 | 476 | 100.00 |
1 | 0.21 | 1 | 0.21 |
2 | 0.42 | 3 | 0.63 |
463 | 97.27 | 466 | 97.90 |
9 | 1.89 | 475 | 99.79 |
1 | 0.21 | 476 | 100.00 |
476 | 100.00 | 476 | 100.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!
Use the 's' modifier with the COMPRESS() function to remove them as well and have no trailing invisible spaces.
Example 3 covers this in the doc.
What are the formats and types associated with the variable?
I ran a PROC CONTENTS statement to get this info.
Type = Char
Len = 255
Format = $255
Informat = $255
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.
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!
Try printing the Lengthn = 13 values with format $hex26. format to see the non printing characters.
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 |
0D0A is CR-LF, carriage return - linefeed. So those are the invisible characters that are part of your mysterious values.
Use the 's' modifier with the COMPRESS() function to remove them as well and have no trailing invisible spaces.
Example 3 covers this in the doc.
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!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.