Help using Base SAS procedures

simple conversion of character variables

Reply
Frequent Contributor
Posts: 75

simple conversion of character variables

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
Super Contributor
Super Contributor
Posts: 3,174

Re: simple conversion of character variables

Look at using the TRANSLATE function - you will need to define argument2 and argument3 with the value mapping/associations.

Scott Barry
SBBWorks, Inc.
Super User
Posts: 3,105

Re: simple conversion of character variables

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.
Super User
Posts: 9,681

Re: simple conversion of character variables

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
Ask a Question
Discussion stats
  • 3 replies
  • 82 views
  • 0 likes
  • 4 in conversation