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;
It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

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
It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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;
It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
claireluen
Calcite | Level 5

Perfect, thanks.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 3846 views
  • 0 likes
  • 3 in conversation