SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Recode values with a suffix

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

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;

View solution in original post


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?

Contributor
Posts: 71

Re: Recode values with a suffix

Posted in reply to mohamed_zaki

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;
Contributor
Posts: 71

Re: Recode values with a suffix

Posted in reply to mohamed_zaki

Thank you Thank you - It worked

Best Regards

Contributor
Posts: 71

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.

Trusted Advisor
Posts: 1,117

Re: Recode values with a suffix

Posted in reply to mohamed_zaki

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.)

Trusted Advisor
Posts: 1,117

Re: Recode values with a suffix

Posted in reply to mohamed_zaki

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 486 views
  • 2 likes
  • 3 in conversation