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

hi,

suppose I have the following table:

role
CEO/CFO
CEO since 1987
President/CEO/Treasurer
Independent Director
First Independent In 2 Years

I want to create 2 new variables CEO_dummy and Independent_dummy. If for example the word CEO is present for an entry in the Role column, then the new variable CEO_dummy will have a value of 1, and 0 otherwise.

The same thing for Independent.


So the new table should look like this:


roleCEO_dummyIndependent_dummy
CEO/CFO10
CEO since 198710
President/CEO/Treasurer11
Independent Director01
First Independent In 2 Years01



Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

If you're searching for WORDS you need the proper function.

data dummy;
   input string $80.;
   d1 = not not findw(string,
'ceo',1,,'TSIEP');
   d2 = not not findw(string,'independent',1,,'TSIEP');
   cards;
CEO/CFO
CEO since 1987
Independent alliaceous
Independently alliaceous
President/CEO/Treasurer
Independent Director
First Independent In 2 Years
;;;;
   run;
proc print;
  
run;

5-13-2015 6-36-54 AM.png

View solution in original post

6 REPLIES 6
samirt
Fluorite | Level 6

Suppose dataset name is dataset1

data dataset1;

     set dataset1;

     if find(role,'CEO','i') > 0 then CEO_dummy=1;

     else Independent_dummy=0;

run;

This will solve the problem.

samirt
Fluorite | Level 6

Addition to the  above code .This code will resolve your problem for missing dummy variables.

data dataset1;

   

IF FIND('role','ceo','i')>0 THEN DO;

        ceo_DUMMY=1;

        INDEPENDENT_DUMMY=0;

   END;

   ELSE IF(FIND(role','ceo','i')=0)  THEN DO;

     ceo_DUMMY=0;

     INDEPENDENT_DUMMY=1;

   END;

run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Your test output doesn't work.  The third line has 1 in both results, but I don't see the word independant in the string?

Its a simple task however:

data want;

     set have;

     if index(lowcase(role),"ceo") > 0 then ceo_dummy=1;

     else ceo_dummy=0;

     if index(lowcase(role),"independent") > 0 then independent=1;

     else independent=0;

run;

pradeepalankar
Obsidian | Level 7

you may use this also:

data want;

set have;

CEO_dummy=IFN(index(upcase(role),'CEO'),1,0);

INDEPENDENT_dummy=IFN(index(upcase(role),'INDEPENDENT'),1,0);

run;

via sql:

proc sql;

create table want as

select role,

case when index(upcase(role),'CEO') then 1 else 0 end as CEO_dummy,

case when index(upcase(role),'INDEPENDENT') then 1 else 0 end as INDEPENDENT_dummy

from have;

quit;

data_null__
Jade | Level 19

If you're searching for WORDS you need the proper function.

data dummy;
   input string $80.;
   d1 = not not findw(string,
'ceo',1,,'TSIEP');
   d2 = not not findw(string,'independent',1,,'TSIEP');
   cards;
CEO/CFO
CEO since 1987
Independent alliaceous
Independently alliaceous
President/CEO/Treasurer
Independent Director
First Independent In 2 Years
;;;;
   run;
proc print;
  
run;

5-13-2015 6-36-54 AM.png
ilikesas
Barite | Level 11

Thanks, now I even realize the importance of Independently alliaceous!

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
  • 6 replies
  • 3959 views
  • 0 likes
  • 5 in conversation