Advertisement
littlefeltfangs

SAS de-encoding problem

Jul 26th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 1.60 KB | None | 0 0
  1. data work.encode;
  2. infile datalines;
  3. input text_enc $60.;
  4. datalines;
  5. ABCABlABjABhAB1ABzABlAAgABVABUABGAA4AAgABpABzAAgABoABhAByABk            
  6. ABTABpABtABwABsABlAByAAgABsABpABrABlAAgAB0ABoABpABz                    
  7. ABJABvAAgABJABvAAgABCAByABvABtABpABvABz                                                
  8. ;
  9. run;
  10.  
  11. /* Set up the functions */
  12. proc fcmp outlib=work.funcs.text;
  13.    function deencode(enc $) $;
  14.         len_enc = length(trim(enc));
  15.         collate = '                                                                                        ';
  16.         if int(len_enc/3) = (len_enc/3) then do;
  17.             array conversion{128} _temporary_ (.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,62,.,.,.,63,52,53,54,55,56,57,58,59,60,61,.,.,.,.,.,.,., 0, 1, 2, 3, 4,5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,.,.,.,.,.,.,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,.,.,.,.,.);
  18.             array final{60} $ /NOSYMBOLS;
  19.             do i=1 to int(len_enc/3);
  20.                 currentpart = substr(enc,(i*3)-2,3);
  21.                 unichar = "\u"!!put(blshift(conversion{1+rank(char(currentpart,1))},12)+blshift(conversion{1+rank(char(currentpart,2))},6)+conversion{1+rank(char(currentpart,3))},hex4.);
  22.                 final{i} = unicode(unichar);
  23.                 collate = trim(collate)!!trim(final{i});
  24.             end;
  25.             *outtext = cat(of final{*});
  26.             return (trim(collate));
  27.         end;
  28.         else return ('');
  29.    endsub;
  30.  
  31. /* Run the code */
  32.  
  33. options cmplib=work.funcs;
  34. data work.function_test;
  35. set work.encode;
  36. length finaltext $400.;
  37. finaltext = deencode(text_enc);
  38. run;
  39.  
  40. proc print data = work.function_test;
  41. run;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement