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

I'm creating dummy variables for the first time!

I am using data from NHANES.

I have a variable smoke_hx where 0= never smoked, 1=past smoker, 2=current smoker. 

I want to create the variables ever_smoked1 that is a dummy for smoke_hx=1 and ever_smoked2 which is a dummy for smoke_hx=2. Never smoked is the reference group. 

 

How do I create these dummy variables? I've read a bunch of explanations but nothing is making sense!

1 ACCEPTED SOLUTION

Accepted Solutions
westbestern
Obsidian | Level 7

Good to know! But for this class, my professor is requiring us to manually create them. This is the code I have. Not sure if it's correct.


@Reeza wrote:
Why?
If the CLASS statement exists with in the procedure it will do the dummy coding for you, no need to do that manually.
data data2;
set data1;
	if smoke_hx=1 then ever_smoked1=1;
	else ever_smoked1=0;
	if smoke_hx=2 then ever_smoked2=1;
	else ever_smoked2=0;
	run;



View solution in original post

3 REPLIES 3
Reeza
Super User
Why?
If the CLASS statement exists with in the procedure it will do the dummy coding for you, no need to do that manually.
westbestern
Obsidian | Level 7

Good to know! But for this class, my professor is requiring us to manually create them. This is the code I have. Not sure if it's correct.


@Reeza wrote:
Why?
If the CLASS statement exists with in the procedure it will do the dummy coding for you, no need to do that manually.
data data2;
set data1;
	if smoke_hx=1 then ever_smoked1=1;
	else ever_smoked1=0;
	if smoke_hx=2 then ever_smoked2=1;
	else ever_smoked2=0;
	run;



Reeza
Super User
Your code looks correct then. You can check it though, using proc freq.

proc freq data=data2;
table smoke_hx*ever_smoked1 / list;
table smoke_hx*ever_smoked2 / list;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 744 views
  • 1 like
  • 2 in conversation