DATA Step, Macro, Functions and more

base64 encoding

Reply
Occasional Contributor
Posts: 7

base64 encoding

Hi all.

Can anybody suggest me the best way to encode one variable in dataset to base64.

Super User
Posts: 10,028

base64 encoding

Posted in reply to attaman2008
Occasional Contributor
Posts: 7

base64 encoding

Thanks, but I need to encode only one variable in dataset, I use SAS 9.1.3. I need something like function or format. I know that there's needed format, but it's available starting from SAS 9.2. Does anyone know a way to do it in 9.1.3 ?

Super User
Super User
Posts: 7,050

Re: base64 encoding

Posted in reply to attaman2008

Here is one method.  You will need to modify it to store the result into a variable instead of writing to the log.

proc format ;

  value base64c

0 = A   16 = Q   32 = g   48 = w

1 = B   17 = R   33 = h   49 = x

2 = C   18 = S   34 = i   50 = y

3 = D   19 = T   35 = j   51 = z

4 = E   20 = U   36 = k   52 = 0

5 = F   21 = V   37 = l   53 = 1

6 = G   22 = W   38 = m   54 = 2

7 = H   23 = X   39 = n   55 = 3

8 = I   24 = Y   40 = o   56 = 4

9 = J   25 = Z   41 = p   57 = 5

10 = K   26 = a   42 = q   58 = 6

11 = L   27 = b   43 = r   59 = 7

12 = M   28 = c   44 = s   60 = 8

13 = N   29 = d   45 = t   61 = 9

14 = O   30 = e   46 = u   62 = '+'

15 = P   31 = f   47 = v  63 = '/'

  ;

run;

data x;

  length string $9;

  string='hello';

  do i=1 by 3 until(i > length(string));

    char4 = put(substr(string||'   ',i,3),binary24.);

    do j=1,7,13,19 ;

      char1 = put(input(substr(char4,j),binary6.),base64c.);

      put char1 $1. @;

    end;

  end;

  base64 = put(string,$base64x12.);

  put / base64 / string=;

run;

Trusted Advisor
Posts: 1,301

Re: base64 encoding

Tom shows the best method.  Base64 encoding in done via format in SAS instead of what you may more intuitivly think would be a function.

Here is just something dumb to convert the format usage to function usage...

proc fcmp outlib=work.func.cipher;

function b64(string $) $;

  length digest $32767;

  digest=strip(put(string,$base64x32767.));

  return(digest);

endsub;

quit;

options cmplib=(work.func);

data _null_;

string='hello';

b64=b64(string);

put b64;

run;

Occasional Contributor
Posts: 7

Re: base64 encoding

Posted in reply to attaman2008

Tom and FriedEgg - thank you very much )

Ask a Question
Discussion stats
  • 5 replies
  • 1764 views
  • 2 likes
  • 4 in conversation