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
Amethyst | Level 16

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
Amethyst | Level 16

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
Amethyst | Level 16

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



hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1057 views
  • 6 likes
  • 2 in conversation