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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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;
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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

I don't understand the request.

--
Paige Miller
claireluen
Calcite | Level 5

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.

ChrisHemedinger
Community Manager

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
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ChrisHemedinger
Community Manager

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;
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
ChrisHemedinger
Community Manager

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.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
claireluen
Calcite | Level 5

Perfect, thanks.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 7 replies
  • 5012 views
  • 0 likes
  • 3 in conversation