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!
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;
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;
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;
thanks everything worked.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.