## Recode values with a suffix

Solved
Frequent Contributor
Posts: 81

# Recode values with a suffix

I want to create a new variable from the old var that have a suffix eg my original var looks like this:

0000001100

0000110011-11

0010010010

0000100001-12

0000100001-14

I want to code the values with "-" to a new variable 1, and all else as "0"

Thanks

Accepted Solutions
Solution
‎01-10-2016 04:58 PM
Super Contributor
Posts: 490

## Re: Recode values with a suffix

[ Edited ]
``````if index(old, "-")> 0 then new=1;
Else new=0;``````

All Replies
Super Contributor
Posts: 490

## Re: Recode values with a suffix

Show us how your desired output will be, based on the example dataset you gave already?

Frequent Contributor
Posts: 81

## Re: Recode values with a suffix

Old_Variable                     New_Variable

0000001100                              0

0000110011-11                         1

0010010010                              0

0000100001-12                         1

0000100001-14                         1

if old_variable has "-" then new_variable = 1;

else "0"

Somthing like this where I can tease out the ones with "-"

Thanks

Solution
‎01-10-2016 04:58 PM
Super Contributor
Posts: 490

## Re: Recode values with a suffix

[ Edited ]
``````if index(old, "-")> 0 then new=1;
Else new=0;``````
Frequent Contributor
Posts: 81

## Re: Recode values with a suffix

Thank you Thank you - It worked

Best Regards

Frequent Contributor
Posts: 81

## Re: Recode values with a suffix

Just confirming this worked

new = index(old, "-");

Super Contributor
Posts: 490

## Re: Recode values with a suffix

Then if "-" found you get the index >=1 based on the first apperance. If that what you want then that is fine.

Posts: 1,157

## Re: Recode values with a suffix

Otherwise, you could prefix the function call with a double NOT to obtain a 0-1 flag: new=~~index(old,'-');

(Or new=~~find(old,'-'); to save one more character.)

Posts: 1,157

## Re: Recode values with a suffix

Or without IF-THEN-ELSE:

`` new_variable=(index(old_variable,'-')>0);``

But you should still keep in mind, that missing values of Old_Variable (if any) would be coded as 0. Depending on what you plan to do with New_Variable, this may or may not be useful.

🔒 This topic is solved and locked.