Desktop productivity for business analysts and programmers

Creating a dummy variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

Creating a dummy variable

I am trying to create a dummy variable where if weekday=1, then Monday=1...

 

 

so far I have this:

data want
set have;
if weekday=1 then mon=1;
else mon=0;
run;

but this does not actually create the variable, what am I missing?

 


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 6,899

Re: Creating a dummy variable

if weekday in (2, 3, 4) then TWT=1;

View solution in original post


All Replies
Respected Advisor
Posts: 3,249

Re: Creating a dummy variable

We would need to see a (portion of) your data. We can't answer your question without it. (Or better yet, YOU look at the data and see if you can figure out why this simple code isn't working, before asking us. Don't actually ask us without YOU looking at the data first to see if you can figure it out)

--
Paige Miller
PROC Star
Posts: 1,400

Re: Creating a dummy variable

Do you have a variable called weekday in your data? Or do you simply have a date variable?

 

If the latter is the case, you should use the WEEKDAY Function instead.

 

Anyway, always a good idea to specify what your data looks like

Frequent Contributor
Posts: 89

Re: Creating a dummy variable

I do have a variable called weekday that goes from 0 to 6
Frequent Contributor
Posts: 89

Re: Creating a dummy variable

nvm I got it working
Frequent Contributor
Posts: 89

Re: Creating a dummy variable

Sorry, one more question:

if this works, if weekday=1 then mon=1;
else mon=0;

then how would I do if weekday=2,3,4 then TWT=1 ??
Solution
2 weeks ago
Super User
Posts: 6,899

Re: Creating a dummy variable

if weekday in (2, 3, 4) then TWT=1;

Frequent Contributor
Posts: 89

Re: Creating a dummy variable

Posted in reply to Astounding
how would I do that if I want a variables that is between two values like between 75 and 85? (75:85) only gives me full values but I have decimals in them so this doesn't work and the statement 'between' does not seem to work
Respected Advisor
Posts: 3,249

Re: Creating a dummy variable

You have to stop telling us "doesn't work". You have to explain what happened and what you expected to happen. You have to show us the SASLOG.

--
Paige Miller
Frequent Contributor
Posts: 89

Re: Creating a dummy variable

Posted in reply to PaigeMiller
25         if weekday=0 then sun=1;
26         else sun=0;
27         if Temperature =< 75 then low=1;
28         else low=0;
29         if Temperature between 75 and 85 then mid=1;
                          _______
                          388
                          202
ERROR 388-185: Expecting an arithmetic operator.

I'm sorry, I just thought it would be enough

 

won't happen again

Super User
Posts: 6,899

Re: Creating a dummy variable

if (75 <= var <= 85) then .......

Super User
Posts: 13,876

Re: Creating a dummy variable

Posted in reply to Astounding

@Astounding wrote:

if weekday in (2, 3, 4) then TWT=1;


Twt= weekday in (2, 3, 4);

 

Avoids need for Else twt=0.

Super User
Posts: 6,899

Re: Creating a dummy variable

Did you read the log?

 

You are missing a semicolon at the end of the DATA statement.  Depending on the setting for system options, using SET as a data set name may or may not generate an error message.  You will have to check to see whether your original data set is intact or not.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 139 views
  • 2 likes
  • 5 in conversation