BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
turcay
Lapis Lazuli | Level 10

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

14 REPLIES 14
ballardw
Super User

When I run your code with SAS 9.2 I do get

session_id=f600fdf7e295e2b9:67a8388a:14e8d79a0e3:4b82

turcay
Lapis Lazuli | Level 10

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

ChrisNZ
Tourmaline | Level 20

I get no missing value.

k=ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw

session_id=f600fdf7e295e2b9:67a8388a:14eccf02dbb:-782

on sas 9.4 win64.

turcay
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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?

turcay
Lapis Lazuli | Level 10

I didn't understand what exactly you mean ? Can you explain it ?

turcay
Lapis Lazuli | Level 10

@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.

Steelers_In_DC
Barite | Level 11

What is it that you are trying to achieve?

Tom
Super User Tom
Super User

Actually this is explained in the documentation.  SAS(R) 9.4 Formats and Informats: Reference

Details

Base 64 is an industry encoding method whose encoded characters are determined by using a positional scheme that uses only ASCII characters. Several Base 64 encoding schemes have been defined by the industry for specific uses, such as email or content masking. SAS maps positions 0–61 to the characters A–Z, a–z, and 0–9. Position 62 maps to the character +, and position 63 maps to the character /.

The following are some uses of Base 64 encoding:

  • embed binary data in an XML file

  • 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;


Tom
Super User Tom
Super User

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

turcay
Lapis Lazuli | Level 10

Thank you for your detailed explanation Tom, it was a nice work. My problem was solved, thanks again.

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 6640 views
  • 6 likes
  • 6 in conversation