Help using Base SAS procedures

Decode from Base64 using $base64x do not work

Accepted Solution Solved
Reply
Super Contributor
Posts: 381
Accepted Solution

Decode from Base64 using $base64x do not work

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?


Accepted Solutions
Solution
‎08-07-2015 12:21 PM
Super User
Super User
Posts: 6,502

Re: Decode from Base64 using $base64x do not work

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


All Replies
Super User
Posts: 10,527

Re: Wrong Missing Value Problem

When I run your code with SAS 9.2 I do get

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

Super Contributor
Posts: 381

Re: Wrong Missing Value Problem

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

PROC Star
Posts: 1,564

Re: Wrong Missing Value Problem

I get no missing value.

k=ZjYwMGZkZjdlMjk1ZTJiOTo2N2E4Mzg4YToxNGVjY2YwMmRiYjotNzgyYw

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

on sas 9.4 win64.

Super Contributor
Posts: 381

Re: Wrong Missing Value Problem

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

Super User
Super User
Posts: 6,502

Re: Wrong Missing Value Problem

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?

Super User
Posts: 6,962

Re: Wrong Missing Value Problem

Second that, on SAS 9.2 (AIXR64) via EG 4.3

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 381

Re: Wrong Missing Value Problem

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

Super User
Posts: 6,962

Re: Wrong Missing Value Problem

That I also get, in my environment, the complete session_id including the trailing "2".

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 381

Re: Wrong Missing Value Problem

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

Valued Guide
Posts: 858

Re: Wrong Missing Value Problem

What is it that you are trying to achieve?

Super User
Super User
Posts: 6,502

Re: Decode from Base64 using $base64x do not work

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;


Solution
‎08-07-2015 12:21 PM
Super User
Super User
Posts: 6,502

Re: Decode from Base64 using $base64x do not work

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

Super Contributor
Posts: 381

Re: Decode from Base64 using $base64x do not work

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

Super User
Super User
Posts: 6,502

Re: Decode from Base64 using $base64x do not work

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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