SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xxformat_com
Barite | Level 11

Here the first character is 56, the second one is C3A9, the third one is 72 and the fourth one is 6F:

 

sasondemand.JPG

 

How do we know that the first character is not 56C3 for example?

Is there a new format or feature which would allow to identify explicitely each character?

Eg. get a value like 56-C3A9-72-6F in a similar way as the e8601da format as alternative to b8601da format for ISO dates.

 

This would be useful as this kind of code helps identifying specific characters... to replace them.

I've seen the kpropdata function but not tested it. It could cover most of the common situations but if a format solution exists, it would be good to know about it.

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

You can also just build yourself a format:

proc FCMP outlib=work.f.p;
  function myHex(text $) $;
    length ret $ 32767;
    do i = 1 to klength(text);
      x = ksubstr(text,i,1);
      ret = catx("-", ret, putc(x,cats("hex",length(x)*2,".")));
    end;
    return(ret);
  endsub;
run;

options cmplib=work.f;

proc format;
  value $ myHex (default=200)
  other = [myHex()]
  ;
run;

data  _null_;
  text = "12ŻÓŁĆ34";
  put text $myHex.;
run;

 

[EDIT] one more approach, more "readable"

proc FCMP outlib=work.f.p;
  function myHex(text $) $;
    length ret $ 32767 x $ 4 t $ 8;
    do i = 1 to klength(text);
      x = ksubstr(text, i, 1);

      select(length(x));
        when (1)  t = put(x, $hex2.);
        when (2)  t = put(x, $hex4.);
        when (3)  t = put(x, $hex6.);
        otherwise t = put(x, $hex8.);
      end;

      ret = catx("-", ret, t);
    end;
    
    return(ret);
  endsub;
run;

 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15

Try this:

data  _null_;
  text = "12ŻÓŁĆ34";
  do i = 1 to klength(text);
    x = ksubstr(text,i,1);
    t = putc(x,cats("hex",length(x)*2,"."));
    put t @@;
  end;
  put;
run;

log shows:

1    data  _null_;
2      text = "12ŻÓŁĆ34";
3      do i = 1 to klength(text);
4        x = ksubstr(text,i,1);
5        t = putc(x,cats("hex",length(x)*2,"."));
6        put t @@;
7      end;
8      put;
9    run;

31 32 C5BB C393 C581 C486 33 34
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

You can also just build yourself a format:

proc FCMP outlib=work.f.p;
  function myHex(text $) $;
    length ret $ 32767;
    do i = 1 to klength(text);
      x = ksubstr(text,i,1);
      ret = catx("-", ret, putc(x,cats("hex",length(x)*2,".")));
    end;
    return(ret);
  endsub;
run;

options cmplib=work.f;

proc format;
  value $ myHex (default=200)
  other = [myHex()]
  ;
run;

data  _null_;
  text = "12ŻÓŁĆ34";
  put text $myHex.;
run;

 

[EDIT] one more approach, more "readable"

proc FCMP outlib=work.f.p;
  function myHex(text $) $;
    length ret $ 32767 x $ 4 t $ 8;
    do i = 1 to klength(text);
      x = ksubstr(text, i, 1);

      select(length(x));
        when (1)  t = put(x, $hex2.);
        when (2)  t = put(x, $hex4.);
        when (3)  t = put(x, $hex6.);
        otherwise t = put(x, $hex8.);
      end;

      ret = catx("-", ret, t);
    end;
    
    return(ret);
  endsub;
run;

 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 875 views
  • 6 likes
  • 2 in conversation