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?
@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;
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;
proc format;
invalue Ind_Fran
'Ind' = 1
'Fran' = 0
other = .;
Run;
So you're saying that this code would not change the datatype?
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.
Value is giving me an error
@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.
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.
@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.
@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.
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.);
Would it be possible then for me to drop type and rename type1 to type?
@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;
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!
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.
Ready to level-up your skills? Choose your own adventure.