BookmarkSubscribeRSS Feed
Jbraun
Fluorite | Level 6

Dear community members

 

This is my first post, so I hope my explanation is useful. I am trying to recode a character variable.

 

I have a dataset called "Wages", a variable called "Level" and in this variable I wish to recode each observation "Level 01" to "01" instead, so that I can later change the variable to a numeric one. Basically, instead of having data with numbers and labels that go with these numbers, I have data where each category is represented by text instead, so I guess I have to do a lot of work to recode all this data and put on labels afterwards before I can start analysing my data.

 

So how do I recode observations while keeping all the other data in my datasheet?

 

I have tried to use SCAN, SUBSTR and PROC SQL but I'm not really getting anywhere. I am getting errors or empty datasheets.

 

Kind regards,

 

Jacob

 

 

 

 

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

I all you want to do is to extract only the numbers in your Level character variabel and convert them to numeric, this may get you started.

 

data have;
input level$1-8 var1 var2;
datalines;
level 01 10 20
level 01 10 40
level 02 10 50
level 03 20 50
level 03 30 20
level 04 30 70
level 04 30 90
;

data want;
	set have;
	num_level=input(compress(level, '', 'kd'), best.);
run;
Astounding
PROC Star

The COMPRESS function can do this easily:

 

data want;

set have;

level = compress(level, , 'kd' ) ;

run;

 

That will keep just the digits.  However, depending on what is in your data, this may be too simple.  You will end up with "01" if your original value is any of these:

 

Level 01

Level 0-1

Level 0.1

Level 0, Sublevel 1

 

Jbraun
Fluorite | Level 6

Thank you very much for your answers. I'm sorry, but I should have specified more clearly: Some of the recodes will not be to just extract the digits. With some observations I have only text which I want to give a number so that I can put a label on that number and do statistical analysis.

 

Kind regards,

Jacob

PeterClemmensen
Tourmaline | Level 20

Please post some example of how your data would look if you want a code answer 🙂

ballardw
Super User

@Jbraun wrote:

Thank you very much for your answers. I'm sorry, but I should have specified more clearly: Some of the recodes will not be to just extract the digits. With some observations I have only text which I want to give a number so that I can put a label on that number and do statistical analysis.

 

Kind regards,

Jacob


Provide examples. If the list of values does not change very often perhaps a custom informat will help.

Jbraun
Fluorite | Level 6

Ok, let me try Again. Below I have tried to explain what my data looks like. I don't have numeric data in my variables, but character variables. The code below would not Work, obviously, but I can't upload the actual data, so I am hoping you understand.

 

When it says "Level 01" I want to recode to a number, such as "1" that I can give a label "Level 01" so that I am dealing with numbers representing categories instead of character observations, since SAS doesn't handle that very well.

 

 

 

 

data Wages;

input Level;

cards;

 

Level 01

Other

Bla.bla.

;

run;

 

Tom
Super User Tom
Super User

Why does it matter if your categorical variable is numeric or character?

What procedure are you trying to use where it makes a difference?

Jbraun
Fluorite | Level 6

@Tom

 

because 'kd' (keep digits) was suggested, and some of my recodes will not be to just extrapolate digits

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
  • 8 replies
  • 1009 views
  • 1 like
  • 5 in conversation