BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aalluru
Obsidian | Level 7

I have a dataset with a column called "Type" which mainly consists of 2 values - "Ind" and "Fran". I would like to make that column numeric such that the value "Ind" is replaced with 1 and the value "Fran" is replaced with 0.

 

Would it be more feasible to use a proc format or to use if-then statements in a data step? Could you give me an example of what the data step would look like considering that the datatype of the column will also have to be changed?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@aalluru wrote:

Would it be possible then for me to drop type and rename type1 to type?


Yes.

data want;
   set have (rename=(var = oldvar));
   var = input(oldvar, informat.);
   drop oldvar;
run;

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

You can use either PROC FORMAT or a data step.

 

PROC FORMAT does not make the variable numeric, the variable remains character, although it appears to us humans as 1 or 0. A data step would require you to create a new numeric variable that contains the numeric 0 and 1 values.

 

data want;
    set have;
    if type='Ind' then type1=1;
    else if type='Fran' then type1=0;
run;

 

--
Paige Miller
aalluru
Obsidian | Level 7
proc format;
invalue Ind_Fran
'Ind' = 1
'Fran' = 0
other = .;
Run;

So you're saying that this code would not change the datatype?

PaigeMiller
Diamond | Level 26

Actually, I was thinking of using VALUE and not INVALUE. VALUE does not change the variable type. I haven't tried it with INVALUE, but I think it leaves the variable type unchanged. Formats and informats just change the appearance of the variable.

 

So you're saying that this code would not change the datatype?

 

The quickest answer is to try it yourself.

--
Paige Miller
aalluru
Obsidian | Level 7

Value is giving me an error

PaigeMiller
Diamond | Level 26

@aalluru wrote:

Value is giving me an error


For your future benefit, please understand that saying something gives an error, and not providing details, never helps. This provides no useful information to diagnose the error.

 

We need to see the LOG for these steps — the entire log for these steps, with nothing chopped out. We need to see the log in a format that preserves the formatting and readability of the log. Copy the log as text, and paste it into the window that appears when you click on the </> icon. I have stopped trying to read logs where these instructions are not followed. DO NOT SKIP THIS STEP.

--
Paige Miller
aalluru
Obsidian | Level 7

oh alright. I am not planning on using format for what I need right now but I will keep that in mind in the future.

Reeza
Super User

@aalluru wrote:

oh alright. I am not planning on using format for what I need right now but I will keep that in mind in the future.


This isn't because someone doesn't want to help you, it's because it could be as simple as a missing semicolon or comma which is super easy to tell when you have the log but almost impossible without it. It helps you get answers faster.

ballardw
Super User

@aalluru wrote:

Value is giving me an error


Once a SAS variable type is set you cannot change it.

The informat would have to be used when either 1) initially reading the data into SAS or 2) create a NEW variable.

Reeza
Super User

No, this creates an informat that can then be applied to a data set or column but changes nothing by itself.

 


@aalluru wrote:
proc format;
invalue Ind_Fran
'Ind' = 1
'Fran' = 0
other = .;
Run;

So you're saying that this code would not change the datatype?


You would need to add an INPUT() statement to get the conversion.

 

newVar = input(oldVar, ind_fran.);

 

aalluru
Obsidian | Level 7

Would it be possible then for me to drop type and rename type1 to type?

ballardw
Super User

@aalluru wrote:

Would it be possible then for me to drop type and rename type1 to type?


Yes.

data want;
   set have (rename=(var = oldvar));
   var = input(oldvar, informat.);
   drop oldvar;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 3158 views
  • 1 like
  • 4 in conversation