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

Hello Folks!

 

Is there any way to keep blank spaces in the end of a string?

 

For example, if I use the code below:

 

data example;

     string_ = 'aaaaa     ';

run;

 

This column in the table will not show the 5 blanks at the end...

 

I have to concatenate some character columns, but the last column I have to fill with blank spaces in the end of the string, and use this string in a MD5 function.

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@Guiluiz 

If this is about md5() then I'd be using the syntax used to populate variable digest2 below.

 

The syntax for digest1 should also work as long as the sum of the lengths of your source variables doesn't exceed a certain threshold.

The MD5() function appears to have a limitation of how long a source string (the message) can be. I'm using in below example 3 SAS variables all with a length of 32KB. It appears that the md5() function doesn't use the whole concatenated string and though returns the same digest value for different combination of source variables. 

data sample;
  attrib  
    var1 var2 var3 length=$32767
    ;
  infile datalines dsd truncover;
  input (var1 var2 var3) (:$char32767.);
  digest1=put(md5(var1||var2||var3),hex32.);
  digest2=put(md5(trim(var1)||'|'||trim(var2)||'|'||trim(var3)),hex32.);
  datalines;
A,B,C
A,  B,C
A,,C
AB, C, 
ABC,,
 AB,C,
;

proc print;
run;

Capture.JPG

 

BTW:

If you're creating this digest value for some scd type 2 change tracking process then I suggest that you only concatenate variables up to the last non-blank value (=building something a bit more dynamic). 

The reason for this: It allows you to later on add additional columns to a table but only for new columns which then also get a value a new change record will get inserted (IF in your logic you add new variables always at the end for concatenation).

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

The trailing spaces are padded with blanks as vlength function indicates???

 

data example;
string_ = 'aaaaa     ';
v=vlength(string_);
run;
Quentin
Super User

Hi,

 

Blanks at the end of a string are preserved by default.  In fact, if you assign a value to a character variable and the value is shorter then the length of the variable, SAS will pad the value with blanks.

 

In below example, STRING1 and STRING2 have the same value (5 a's followed by 5 blanks).  STRING3 has a different value (5 a's with no trailing blanks).  As expected, MD5() returns the same value for STRING1 and STRING2, and different value for STRING3

 

1    data a;
2       length string2 $10 ;
3       string1 = 'aaaaa     '; * $10 ;
4       string2 = 'aaaaa';      * $10 ;
5       string3 = 'aaaaa';      * $5  ;
6       hash1=md5(string1);
7       hash2=md5(string2);
8       hash3=md5(string3);
9       put string1=/ hash1=$hex32.;
10      put string2=/ hash2=$hex32.;
11      put string3=/ hash3=$hex32.;
12   run;

string1=aaaaa
hash1=410A99712BE19FDCBA336C42D1E81684
string2=aaaaa
hash2=410A99712BE19FDCBA336C42D1E81684
string3=aaaaa
hash3=594F803B380A41396ED63DCA39503542

HTH,

Q.

ChrisNZ
Tourmaline | Level 20

There will be as many spaces at the end of the string as the variable length allows.

 

So if the variable is $10 the string is always

A='aaaaa     ';

If it is $5 it is

A='aaaaa';

 

This true regardless of the code. Only the variable length decides on the value.

length A $10; A='a';

yields   A='a         ';

length A $1; A='aaaa';

yields   A='a';

 

Kurt_Bremser
Super User

@Guiluiz wrote:

Hello Folks!

 

Is there any way to keep blank spaces in the end of a string?

 

For example, if I use the code below:

 

data example;

     string_ = 'aaaaa     ';

run;

 

This column in the table will not show the 5 blanks at the end...

 

I have to concatenate some character columns, but the last column I have to fill with blank spaces in the end of the string, and use this string in a MD5 function.

 

Thanks!

 


It's in the nature of blanks that they are invisible 😉

But they are there, see this proof:

data example;
string_ = 'aaaaa     ';
hex_string = put(string_,$hex20.);
put hex_string=;
run;

Log:

24         data example;
25         string_ = 'aaaaa     ';
26         hex_string = put(string_,$hex20.);
27         put hex_string=;
28         run;

hex_string=61616161612020202020

If you want to make sure that SAS writes the blanks to an external file (and does not cut a line short), use the $CHAR10. format for your string.

Patrick
Opal | Level 21

@Guiluiz 

If this is about md5() then I'd be using the syntax used to populate variable digest2 below.

 

The syntax for digest1 should also work as long as the sum of the lengths of your source variables doesn't exceed a certain threshold.

The MD5() function appears to have a limitation of how long a source string (the message) can be. I'm using in below example 3 SAS variables all with a length of 32KB. It appears that the md5() function doesn't use the whole concatenated string and though returns the same digest value for different combination of source variables. 

data sample;
  attrib  
    var1 var2 var3 length=$32767
    ;
  infile datalines dsd truncover;
  input (var1 var2 var3) (:$char32767.);
  digest1=put(md5(var1||var2||var3),hex32.);
  digest2=put(md5(trim(var1)||'|'||trim(var2)||'|'||trim(var3)),hex32.);
  datalines;
A,B,C
A,  B,C
A,,C
AB, C, 
ABC,,
 AB,C,
;

proc print;
run;

Capture.JPG

 

BTW:

If you're creating this digest value for some scd type 2 change tracking process then I suggest that you only concatenate variables up to the last non-blank value (=building something a bit more dynamic). 

The reason for this: It allows you to later on add additional columns to a table but only for new columns which then also get a value a new change record will get inserted (IF in your logic you add new variables always at the end for concatenation).

s_lassen
Meteorite | Level 14

As others have already written, SAS keeps the blanks in the end of a character variable, but they are not always displayed. But that does not mean that they are not there:

13   data _null_;
14      y=md5('abc');
15      z=md5('abc ');
16      put y=$hex32.;
17      put z=$hex32.;
18   run;

y=900150983CD24FB0D6963F7D28E17F72
z=28A53E303DA9F5742476FD6B62434540

As you can see, the two values are different, because of the trailing blank in the second expression.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 7137 views
  • 7 likes
  • 7 in conversation