DATA Step, Macro, Functions and more

recode text

Reply
Occasional Contributor
Posts: 8

recode text

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

 

 

 

 

PROC Star
Posts: 1,215

Re: recode text

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;
Super User
Posts: 6,637

Re: recode text

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

 

Occasional Contributor
Posts: 8

Re: recode text

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

PROC Star
Posts: 1,215

Re: recode text

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

Super User
Posts: 13,338

Re: recode text


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.

Occasional Contributor
Posts: 8

Re: recode text

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;

 

Super User
Super User
Posts: 7,938

Re: recode text

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

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

Occasional Contributor
Posts: 8

Re: recode text

@Tom

 

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

Ask a Question
Discussion stats
  • 8 replies
  • 208 views
  • 1 like
  • 5 in conversation