How do I create a dummy or indicator variable for "sweetness" variable for the data below? Say use 1="2" and 0="4"?
data brand;
input preference moisturecontent sweetness @@;
datalines;
64 4 2 73 4 4 61 4 2 76 4 4 72 6 2 80 6 4
71 6 2 83 6 4 83 8 2 89 8 4 86 8 2 93 8 4
88 10 2 95 10 4 94 10 2 100 10 4
;
run;
proc print;
run;
data want;
set brand;
if sweetness=2 then dummy=1;
else if sweetness=4 then dummy=0;
else;
run;
proc print data=want;
run;
data brand;
input preference moisturecontent sweetness @@;
datalines;
64 4 2 73 4 4 61 4 2 76 4 4 72 6 2 80 6 4
71 6 2 83 6 4 83 8 2 89 8 4 86 8 2 93 8 4
88 10 2 95 10 4 94 10 2 100 10 4
;
run;
data dummy;
set brand;
indicator_sweet=sweetness=2;
run;
data want;
set brand;
if sweetness=2 then dummy=1;
else if sweetness=4 then dummy=0;
else;
run;
proc print data=want;
run;
So, I am certainly reading between the lines here, but it seems to me that you have asked the wrong question, and the answers you received don't steer you any closer to a better solution.
@JUMMY you have asked a huge number of questions here in this forum about modeling in SAS. So why would you need dummy variables? You probably don't. Almost every SAS modeling procedure does not need dummy variables to be created before you do the modeling. The SAS procedures have a CLASS statement, which does the unpleasant and tedious work of creating dummy variables for you. So, bottom line, don't create the dummy variables yourself if you are modeling in almost every SAS modeling procedure, use the CLASS statement.
Next topic:
So ... not reading between the lines here ... again, I'm not happy with the answers you received ...
How do I create a dummy or indicator variable for "sweetness" variable for the data below? Say use 1="2" and 0="4"?
Generally, there's no reason to do this either. You don't need dummy variables. You already have two "class" levels, "2" and "4". Converting to zeros and ones is just extra work that provides no benefit if you are going to treat them as categories.
Bingo!!!
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 25. 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.