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

Hi All - 

The SHA256 function seems to work if I pass it a value directly - output from below is:

43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9

It is 100% correct SHA256 value.

data hashingtest;

length hash $ 64;
format hash $hex64.;

set HP_LEADS.lm_leads_details;

hash = SHA256('A00783126');

put hash;

run;

But if I try to SHA256 an entire column of data the values are wrong and in some cases are way short of expected value length - same account number from columnar results - with code below:

ED9440F53E640CD7588D92160103734E33770E5AA6158C9991B5E558686F5780

 

data hashingtest;

length hash $ 64;
format hash $hex64.;

set HP_LEADS.lm_leads_details;

hash = SHA256(accountnumber);

put hash;

run

I am not a SAS expert by any means but would appreciate any direction on this one.

I've checked my values at this website https://passwordsgenerator.net/sha256-hash-generator/

And doublecheck results using a Microsoft Access macro (no laughing :-|)

So I'm confident the second code set results are not SHA256.

 

Thanks in advance,

 

Patrick

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

I did some experiment:

data  have;
  a1 = 'A00783126';
run;

data _null_;
  set have;

  h1 = SHA256('A00783126');
  h2 = SHA256(a1);

  length h3 h4 $ 64;

  h3 = SHA256('A00783126');
  h4 = SHA256(a1);

  h5 = HASHING('SHA256', 'A00783126');
  h6 = HASHING('SHA256', a1);

  put 
  h1= $hex64.
  /
  h2= $hex64. 
  /
  h3= $hex64. 
  /
  h4= $hex64.
  /
  h5=
  /
  h6= 
  ;
run;

and the result was:

h1=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h2=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h3=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h4=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h5=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h6=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

Looks fine to me.

 

Are the data you have proper? Maybe try to add strip() function to remove leading and trailing blanks.

Additional spaces at the end changes the result:

data _null_;
  set have;

  h1 = SHA256('A00783126');
  h7 = SHA256('A00783126   ');

  put 
  h1= $hex64.
  /
  h7= $hex64.
  ;
run;

log:

h1=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h7=73BC4975C5E495EF2096E860423C80FC19E1C0805E889F8823CB8917F8FA5541
NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

All the best

Bart

 

 

 

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15

I did some experiment:

data  have;
  a1 = 'A00783126';
run;

data _null_;
  set have;

  h1 = SHA256('A00783126');
  h2 = SHA256(a1);

  length h3 h4 $ 64;

  h3 = SHA256('A00783126');
  h4 = SHA256(a1);

  h5 = HASHING('SHA256', 'A00783126');
  h6 = HASHING('SHA256', a1);

  put 
  h1= $hex64.
  /
  h2= $hex64. 
  /
  h3= $hex64. 
  /
  h4= $hex64.
  /
  h5=
  /
  h6= 
  ;
run;

and the result was:

h1=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h2=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h3=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h4=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h5=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h6=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

Looks fine to me.

 

Are the data you have proper? Maybe try to add strip() function to remove leading and trailing blanks.

Additional spaces at the end changes the result:

data _null_;
  set have;

  h1 = SHA256('A00783126');
  h7 = SHA256('A00783126   ');

  put 
  h1= $hex64.
  /
  h7= $hex64.
  ;
run;

log:

h1=43E7361624A78AE87E9F8F36D1E2E88D92C861DFDE61672FA301952A1DE044D9
h7=73BC4975C5E495EF2096E860423C80FC19E1C0805E889F8823CB8917F8FA5541
NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

All the best

Bart

 

 

 

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



pryan75
Calcite | Level 5

@yabwon 

Thanks a tonne - heavily borrowing from you - this works like a charm:

data lm_leads_hash;

  set HP_LEADS.lm_leads_details;
 length h1 $ 64;
  h1 = SHA256(strip(accountnumber));

  put 
  h1= $hex64.

  ;
run;

The results in the log file are perfect - that output table is a mess - but the values have been hashed.

Thanks and have a great weekend!

Patrick

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 812 views
  • 1 like
  • 2 in conversation