BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Gopi2
Fluorite | Level 6

Hi Please help, thanks in advance. 

I want to covert one of my columns that contains a character type in the form of the following --> e2/e2, e2/e3, e2/e4, e3/e3, e3/e4, and e4/e4 ( $5. format). I plan to convert this to the numeric variable before analysis. 

such that 

e2/e2 , e2/e3 and  e3/e3  will be coded as 1

e2/e4, e3/e4 and e4/e4 will be coded as 2

 

I am not sure how to do this!

Please help. thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You can also use a simple IF/THEN:

data want;
set have;
if charvar in ("e2/e2","e2/e3","e3/e3")
then numvar = 1;
else if charvar in ("e2/e4","e3/e4","e4/e4")
then numvar = 2;
run;

or a data step SELECT:

data want;
set have;
select (charvar);
  when ("e2/e2","e2/e3","e3/e3") numvar = 1;
  when ("e2/e4","e3/e4","e4/e4") numvar = 2;
  otherwise;
end;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Create an informat:

data have;
input charvar $;
datalines;
e2/e2
e2/e3
e3/e3
e2/e4
e3/e4
e4/e4
;

proc format;
invalue convert
  "e2/e2" = 1
  "e2/e3" = 1
  "e3/e3" = 1
  "e2/e4" = 2
  "e3/e4" = 2
  "e4/e4" = 2
;
run;

data want;
set have;
numvar = input(charvar,convert.);
run;
Gopi2
Fluorite | Level 6
thanks a lot for your quick response!
Kurt_Bremser
Super User

You can also use a simple IF/THEN:

data want;
set have;
if charvar in ("e2/e2","e2/e3","e3/e3")
then numvar = 1;
else if charvar in ("e2/e4","e3/e4","e4/e4")
then numvar = 2;
run;

or a data step SELECT:

data want;
set have;
select (charvar);
  when ("e2/e2","e2/e3","e3/e3") numvar = 1;
  when ("e2/e4","e3/e4","e4/e4") numvar = 2;
  otherwise;
end;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1222 views
  • 4 likes
  • 2 in conversation