BookmarkSubscribeRSS Feed
byeh2017
Quartz | Level 8

Hello,

 

I have a dataset with a variable that is suppose to only be numeric from 1 through 4, but has some extra variables like A B C and D. I am trying to change those alphabetical variables to the following:

 

A = 1; B = 2; C = 3; D = 4;

 

What exactly is the sas code for this? Here's a sample dataset Thank you.

 

data CDC.SAMPLEEDUCATION;
  infile datalines dsd truncover;
  input Education_Level:$1.;
datalines4;
1
2
3
4
1
2
3
4
1
4
A
B
C
D
;;;;
3 REPLIES 3
LinusH
Tourmaline | Level 20
There are many ways. The translate function, the case statement in SQL, to name a few.
Data never sleeps
collinelliot
Barite | Level 11

A custom informat would do what you want directly in the data step:

 

proc format;
    invalue fixMe
        'A' = 1
        'B' = 2
        'C' = 3
        'D' = 4
        OTHER = [best.];
run;
 

data  SAMPLEEDUCATION;
  infile datalines dsd truncover;
  input Education_Level :fixMe.;
datalines4;
1
2
3
4
1
2
3
4
1
4
A
B
C
D
;;;;
rogerjdeangelis
Barite | Level 11


If A=1,B=2 -- Z=26 and you character code is ascii

data SAMPLEEDUCATION;
  input Education_Level $1.;
  if anyalpha(education_level) then num=rank(Education_Level) -64;
  else num=input(Education_Level,1.);
cards4;
1
2
3
4
1
2
3
4
1
4
A
B
C
D
;;;;
run;quit;


Up to 40 obs WORK.SAMPLEEDUCATION total obs=14

       EDUCATION_
Obs      LEVEL       NUM

  1        1          1
  2        2          2
  3        3          3
  4        4          4
  5        1          1
  6        2          2
  7        3          3
  8        4          4
  9        1          1
 10        4          4
 11        A          1
 12        B          2
 13        C          3
 14        D          4



sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 907 views
  • 5 likes
  • 4 in conversation