BookmarkSubscribeRSS Feed
attaman2008
Calcite | Level 5

Hi all.

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

5 REPLIES 5
attaman2008
Calcite | Level 5

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 ?

Tom
Super User Tom
Super User

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;

FriedEgg
SAS Employee

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;

attaman2008
Calcite | Level 5

Tom and FriedEgg - thank you very much )

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 7381 views
  • 3 likes
  • 4 in conversation