Desktop productivity for business analysts and programmers

Monthly Dummy Variables

Accepted Solution Solved
Reply
Highlighted
Occasional Contributor
Posts: 14
Accepted Solution

Monthly Dummy Variables

What's the easiest way to create monthly dummy variables in SAS Enterprise Guide?  I already have a monthly variable 1-12.  Thanks!


Accepted Solutions
Solution
‎06-06-2014 12:44 PM
Community Manager
Posts: 2,077

Re: Monthly Dummy Variables

Ah, with your clarification it's even a bit easier than my original.  You have a "Month" var that is set to 1 thru 12 already?

data setup (drop=n);
    length mon 8;
   
/* create 100 random-month dates */
   
do n = 1 to 100;
        mon = max(
1,round(ranuni(0)*12,1));
        output;
   
end;
run;

/* fill in the dummy var values */
data create_dummy (drop=i);
set setup;
length m1-m12 8;
array dummy m1-m12;
do i = 1 to 12;
 
/* yields 1 if the var number is the month, else 0 */
  dummy(i) = (mon = i);
end;
run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,071

Re: Monthly Dummy Variables

You want to create "monthly dummy variables" but then you say "I already have monthly dummy variables".

I don't understand the request.

Occasional Contributor
Posts: 14

Re: Monthly Dummy Variables

Sorry, I just corrected. I have a monthly variable.

I know how to do it using query builder to recode the monthly variable column 11 different times.  Surely there is something easier.

Community Manager
Posts: 2,077

Re: Monthly Dummy Variables

Something like this?

data setup (drop=n);
    length date 8;
   
length m1-m12 8;
   
format date date9.;

   
/* create 100 random-month dates */
   
do n = 1 to 100;
        date = mdy(max(
1,round(ranuni(0)*12,1)),1,2014);
        output;
   
end;
run;

/* fill in the dummy var values */
data create_dummy (drop=i);
set setup;
array dummy m1-m12;
do i = 1 to 12;
 
/* yields 1 if the var number is the month, else 0 */
  dummy(i) = (month(date) = i);
end;
run;

Yields:
mondum.png
Chris
Trusted Advisor
Posts: 1,071

Re: Monthly Dummy Variables

claireluen wrote:

I know how to do it using query builder to recode the monthly variable column 11 different times.  Surely there is something easier.

I don't know if this is available through Enterprise Guide, but the easiest way I know of the create dummy variables is PROC GLMMOD.

Solution
‎06-06-2014 12:44 PM
Community Manager
Posts: 2,077

Re: Monthly Dummy Variables

Ah, with your clarification it's even a bit easier than my original.  You have a "Month" var that is set to 1 thru 12 already?

data setup (drop=n);
    length mon 8;
   
/* create 100 random-month dates */
   
do n = 1 to 100;
        mon = max(
1,round(ranuni(0)*12,1));
        output;
   
end;
run;

/* fill in the dummy var values */
data create_dummy (drop=i);
set setup;
length m1-m12 8;
array dummy m1-m12;
do i = 1 to 12;
 
/* yields 1 if the var number is the month, else 0 */
  dummy(i) = (mon = i);
end;
run;
Community Manager
Posts: 2,077

Re: Monthly Dummy Variables

And if you want a more "query-friendly" method, create a 12-row reference table (one for each month value).  Example DATA step:

data ref(keep=month m1-m12);
    length month 8;
   
length m1-m12 8;
   
array dummy m1-m12;
    do outer = 1 to 12;
        month=outer;
       
do inner = 1 to 12;
            dummy(inner) = (outer = inner);
       
end;
       
output;
   
end;
run;

And then JOIN that table with the original, using MONTH as the key.

Occasional Contributor
Posts: 14

Re: Monthly Dummy Variables

Perfect, thanks.

Post a Question
Discussion Stats
  • 7 replies
  • 1251 views
  • 0 likes
  • 3 in conversation