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
if index(old, "-")> 0 then new=1;
Else new=0;
Show us how your desired output will be, based on the example dataset you gave already?
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
if index(old, "-")> 0 then new=1;
Else new=0;
Thank you Thank you - It worked
Best Regards
Just confirming this worked
new = index(old, "-");
Then if "-" found you get the index >=1 based on the first apperance. If that what you want then that is fine.
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.)
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.
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.
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.