Hello,
When I try to decode from base64 as in the following code, it gives me no error but a wrong missing value.
--------------------------
%let id_all ='ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGU4ZDc5YTBlMzo0Yjgy:FwmOGh-t4mFlRKfLk-lsIQ:0AR0kxBhalLe5mi4kI-MjA';
%let id_base = %scan(&id_all,1,':');
%put k=&id_base;
data _null_;
call symput("session_id",input("&id_base.",$base64x64.));
run;
%put session_id=&session_id;
--------------------------
session_id is assigned as "f600fdf7e295e2b9:67a8388a:14e8d79a0e3:4b8" with "2" missing instead of "f600fdf7e295e2b9:67a8388a:14e8d79a0e3:4b82".
What is the reason for that?
Here is macro only solution.
%let id_all ='ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw:Fwm...';
%let session_id = %sysfunc(inputc(%scan(&id_all,1,':')===,$base64x80));
%put &=session_id;
SESSION_ID=f600fdf7e295e2b9:67a8388a:14eccf02dbb:-782c
When I run your code with SAS 9.2 I do get
session_id=f600fdf7e295e2b9:67a8388a:14e8d79a0e3:4b82
Hi there,
Sorry for confusion. Can you please also try to test it with the following code? It results in "c" missing in my environment.
-------------------------------------------------
%let id_all ='ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw:itCgBU8hGLnXH2K6TorBYg:tqQ1fcMMtt9MaHDC57A9Xw';
%let id_base = %scan(&id_all,1,':');
%put k=&id_base;
data _null_;
call symput("session_id",input("&id_base.",$base64x80.));
run;
%put session_id=&session_id;
--------------------------------------------------
Below, you can see the information required. Please let me know if you need further information.
Operating System: LIN X64 .
For Base SAS Software ...
Custom version information: 9.3_M2
Image version information: 9.03.01M2P080112 For SAS/STAT ...
Custom version information: 12.1
Image version information: 9.03.01M0P081512 For SAS/GRAPH ...
Custom version information: 9.3_M2
For SAS/CONNECT ...
Custom version information: 9.3_M2
For SAS Real-Time Decision Manager ...
Custom version information: 5.6
Image version information: 9.03.01M0P042413 For SAS Integration Technologies ...
Custom version information: 9.3_M2
For SAS/ACCESS Interface to Oracle ...
Custom version information: 9.3_M1
I get no missing value.
k=ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw
session_id=f600fdf7e295e2b9:67a8388a:14eccf02dbb:-782
on sas 9.4 win64.
In second case, it should not be missing actually. Decode from base64 includes “c” at the end( https://www.base64decode.org/ ). Somehow it does not in the results.
String = ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw
Desired Result = f600fdf7e295e2b9:67a8388a:14eccf02dbb:-782c
Not Desired Result = f600fdf7e295e2b9:67a8388a:14eccf02dbb:-782
I would raise the issue with SAS technical support and see if they have an explanation.
If use your expected result and encode it using the $BASE64X. format then I get a value with two equal signs on the end.
ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw==
If I decode that on the web site or with $BASE64X informat I get your expected value.
Your original string was length 58 bytes. Is there some reason why inputs to BASE64 should be padded to a certain number or multiple of bytes?
Second that, on SAS 9.2 (AIXR64) via EG 4.3
I didn't understand what exactly you mean ? Can you explain it ?
That I also get, in my environment, the complete session_id including the trailing "2".
@KurtBremser in which environment do you receive them as complete? ( sas version, operationg system, EG or Base SAS etc)
@Tom It comes in this way from REST service and it is a situation where we must get the session id.
What is it that you are trying to achieve?
Actually this is explained in the documentation. SAS(R) 9.4 Formats and Informats: Reference
The following are some uses of Base 64 encoding:
encode passwords
encode URLs
The '=' character in the encoded results indicates that the results have been padded with zero bits. In order for the encoded characters to be decoded, the '=' must be included in the value to be decoded.
So your program needs to pad the input string with equal signs until the length is a multiple of 3.
data _null_;
id_all ='ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw:Fwm...';
id_base = scan(id_all,1,':');
session_id = inputc(cats(id_base,'==='),cats('$base64x',3*ceil(length(id_base)/3),'.'));
put (_all_) (=/);
run;
Here is macro only solution.
%let id_all ='ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw:Fwm...';
%let session_id = %sysfunc(inputc(%scan(&id_all,1,':')===,$base64x80));
%put &=session_id;
SESSION_ID=f600fdf7e295e2b9:67a8388a:14eccf02dbb:-782c
Thank you for your detailed explanation Tom, it was a nice work. My problem was solved, thanks again.
For any given string you just need 0, 1 or 2 equal signs to pad it out to a multiple of 3, but always adding 2 or more seems to work fine since the extra binary zeros are ignored.
You should probably raise it with SAS as a issue via the "submit a problem report" link in the footnotes at the bottom of the forum pages.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.