Search code examples
sassha

SAS lowercase SHA256 string


Currently testing a SHA256 function in order to prepare a variable for use in another application.

The user has requested the SHA256 result be in lower case. I created a quick record in order to make sure I can convert the string-

data have;
input first $ last $ dob $ 10. sex $;
cards;
test person 1955-07-31 1
;
run;

Seems it will not allow a lower case string once passed through the SHA function.

Is there a workaround for this? The below attempt did not yield desirable results.

data have2;
set have;
source = catt(first,last,dob,sex);
encryp = lowcase(sha256(source));
format encryp $hex64.;
run;

Solution

  • Below will do it using a put statement with the format inside:

    data have2;
    set have;
    encryp = lowcase(put(sha256(catt(first,last,dob,sex)),$hex64.));
    run;
    

    It will show an entirely different encryption code compared to your method but it remains consistent.

    data have;
    input first $ last $ dob $ 10. sex $;
    cards;
    test person 1955-07-31 1
    test person 1955-07-31 1
    test2 person 1977-08-11 2
    test3 person 1945-12-22 1
    ;
    run;
    
    data have2;
    set have;
    new_encryp = lowcase(put(sha256(catt(first,last,dob,sex)),hex64.)); /* new method */
    encryp = lowcase(sha256(catt(first,last,dob,sex))); /* what you tried */
    format encryp $hex64.;
    run;
    
    /* output */
    first   last    dob         sex new_encryp                                                          encryp
    test    person  7/31/1955   1   038a855a47f40edf54094adc4366e3e79c1a931346d7968e96d2cb930b01e7bc    039A857A67F40EDF74096AFC6366E3E79C1A931366D7969E96F2EB930B01E7BC
    test    person  7/31/1955   1   038a855a47f40edf54094adc4366e3e79c1a931346d7968e96d2cb930b01e7bc    039A857A67F40EDF74096AFC6366E3E79C1A931366D7969E96F2EB930B01E7BC
    test2   person  8/11/1977   2   1117ab614f48a7edfbe9d615f12acad9d564b457b0f31bb2619f7eb9b10f1e58    1117AB616F68A7EDFBE9F615F12AEAF9F564B477B0F31BB261FF7EB9B10F1E78
    test3   person  12/22/1945  1   d1cb00ebe044c0553039f99592dc7bd4804eac2c13da8208fd82459c3a37efd1    F1EB00EBE064E0753039F99592FC7BF4806EAC2C13FA8208FD82659C3A37EFF1