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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11
if index(old, "-")> 0 then new=1;
Else new=0;

View solution in original post

8 REPLIES 8
mohamed_zaki
Barite | Level 11

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

AZIQ1
Quartz | Level 8

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

mohamed_zaki
Barite | Level 11
if index(old, "-")> 0 then new=1;
Else new=0;
AZIQ1
Quartz | Level 8

Thank you Thank you - It worked

Best Regards

AZIQ1
Quartz | Level 8

Just confirming this worked 

 

new = index(old, "-");

mohamed_zaki
Barite | Level 11

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

FreelanceReinh
Jade | Level 19

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

FreelanceReinh
Jade | Level 19

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.

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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