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

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!

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