I have a string variable in the form as below:
delivery
air
ship
ship
air
air
NA
NA
ship
air
air
I am trying to convert the above strings with non-numeric values such that air is assigned a label value of 0 and ship is assigned a label value of 1, while also recognising NA as missing value.
So far I have tried destring/generate, resulting in the following error:
[destring delivery, ignore ("NA") gen (delivery_new)]
[delivery: contains characters not specified in ignore(); no generate]
Similarly, I tried encode, which converted strings to numeric successfully, but still does not recognise the missing values:
[label define deliverylabel 0 "ship" 1 "air"
encode delivery, gen (delivery_new) label (deliverylabel)]
I am not sure what is the best way to tackle this. Any insights will be hugely appreciated.
Thanks,
Keshab
Related Posts with Non-numeric string variable with missing value to numeric variable that recognises the missing value
Herfindahl-Hirschman Index for banksHello, I have a problem creating Herfindahl-Hirschman Index for my data I tried scc install hhi but…
Importing csvHi, I have an easy problem, but unfortunately I could not solve it. When I want to import data fro…
Confidence interval for graphsHi all, I am plotted a connected graph using this code: Code: twoway (connected weekmeanifference …
removing text from string variable with date formatHello, I have a string variable that looks like this: 13-03-2014(Late visited). How can I remove som…
Looping a regression command sequentially by observation and storing each model.Hi all, Apologies if this is very simple or has been asked before. I have searched through many top…
Subscribe to:
Post Comments (Atom)
0 Response to Non-numeric string variable with missing value to numeric variable that recognises the missing value
Post a Comment