SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Jose7
Obsidian | Level 7

Hi experts

 

I'd like to convert a normal text to ascii code, for example: in my dataset I have the letter "t" and the value that i want is 116 and so on with the other letters or numbers.

 

do you know a way to do it?  the final result that I want is a file in that format.

2 REPLIES 2
Tom
Super User Tom
Super User

The easiest way is to display the values in HEX instead of decimal.

data have;
  input string $20.;
cards;
The rain in Spain
falls mainly on 
the plain.
;

data want;
  set have;
  codes = put(string,$hex40.);
run;

Results

Obs    string                                codes

 1     The rain in Spain    546865207261696E20696E20537061696E202020
 2     falls mainly on      66616C6C73206D61696E6C79206F6E2020202020
 3     the plain.           74686520706C61696E2E20202020202020202020

Patrick
Opal | Level 21

The rank function returns the ascii code for a character.

data _null_;
  file print;
  ascii_code=rank('t');
  put ascii_code;
run;

Patrick_1-1729730268882.png

 

the final result that I want is a file in that format.

Like a text file with all the characters replaced with their respective ASCII code? And I guess that all ASCII codes would need to take-up 3 positions so you know where they start and end.

Why ASCII codes which is very unusual and not HEX values?

 

....and just thinking: May-be tell us what the purpose for such conversion is. There might be other ways for achieving the same goal.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 538 views
  • 2 likes
  • 3 in conversation