BookmarkSubscribeRSS Feed
trekvana
Calcite | Level 5
hello all-

say i have a character variable x=(a,a,a,a,c,c,b,b,b,d,d,e) how can i convert this to y=(1,1,1,1,2,2,3,3,3,4,4,5).

ive tried the input command but im not sure how to use this. im assuming there must be an easy way to do this

cheers
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Look at using the TRANSLATE function - you will need to define argument2 and argument3 with the value mapping/associations.

Scott Barry
SBBWorks, Inc.
SASKiwi
PROC Star
I would be inclined to create an custom INFORMAT to do this:

proc format;
invalue alpha_order 'A' = 1;
run;

data test;
var = 'A';
var_order = input(var, alpha_order.);
put _all_;
run;

You could expand this example to cover all 26 alphabet letters, or write a DATA step to generate a PROC FORMAT input dataset automatically.
Ksharp
Super User
Just as SBB said.Try this.
[pre]
data a;
input x $;
datalines;
a
a
a
a
c
c
b
b
b
d
d
e
;

data b;
set a;
_x=translate(x,'12345','abcde');
run;
proc print;
run;
[/pre]



Ksharp

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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