Hello,
I'm looking for an efficient way to create dummy variables and I've seen this example online:
DATA auto ; LENGTH make $ 20 ; INPUT make $ 1-17 price mpg rep78 ; CARDS; AMC Concord 4099 22 3 AMC Pacer 4749 17 3 Audi 5000 9690 17 5 Audi Fox 6295 23 3 BMW 320i 9735 25 4 Buick Century 4816 20 3 Buick Electra 7827 15 4 Buick LeSabre 5788 18 3 Cad. Eldorado 14500 14 2 Olds Starfire 4195 24 1 Olds Toronado 10371 16 3 Plym. Volare 4060 18 2 Pont. Catalina 5798 18 4 Pont. Firebird 4934 18 1 Pont. Grand Prix 5222 19 3 Pont. Le Mans 4723 19 3 ; RUN;
DATA auto2; set auto; ARRAY dummys {*} 3. rep78_1 - rep78_5; DO i=1 TO 5; dummys(i) = 0; END; dummys( rep78 ) = 1; RUN;
This has been working for me except when I have missing values (e.g. if rep78 has missing values). Is there a way to adjust this code to account for the missing values or do I need a different solution?
Thanks
DATA auto2;
set auto;
ARRAY dummys rep78_1 - rep78_5 (5*0);
if not missing(rep78) then dummys( rep78 ) = 1;
RUN;
This gives your dummy variables a value of zero when rep78 is missing. You can modify that further to give the dummy variables a missing value as well.
DATA auto2;
set auto;
ARRAY dummys rep78_1 - rep78_5 (5*0);
if not missing(rep78) then dummys( rep78 ) = 1;
RUN;
This gives your dummy variables a value of zero when rep78 is missing. You can modify that further to give the dummy variables a missing value as well.
Hello,
You mentioned that 'You can modify that further to give the dummy variables a missing value as well.' Can you show me that modification?
Thank you.
Replace the 0 with a .
Make sure the value of REP78 is something you could use as an index into the array.
if rep78 in (1:5) then dummys( rep78 ) = 1;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: