Help using Base SAS procedures

creating dummies (automatically)

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

creating dummies (automatically)

I have data with month variable. For each month, I want to create a dummy variable. Can someone tell me how I can do this automatically?

I think teh following should do the job, but it doesn't. Anyone knows why?

THanks,

C

DATA auto3;

  set auto3;

  ARRAY dummys {*} 8.  month_1 - month_12;

  DO i=1 TO 12;                                   

  dummys(i) = 0;

  END;

  dummys(month) = 1;

RUN;


Accepted Solutions
Solution
‎10-28-2011 10:32 AM
PROC Star
Posts: 7,484

creating dummies (automatically)

You will have to post what auto3 initially looks like and be more specific about what you want to achieve.

The way your code is written, the twelve variables month_1-month_12 would have to already exist.  You may be looking for something like:

DATA auto3;

  set auto3;

  ARRAY month_ {12};

  DO i=1 TO 12;                                  

    month_(i) = ifn(i=month,1,0);

  END;

RUN;

View solution in original post


All Replies
Solution
‎10-28-2011 10:32 AM
PROC Star
Posts: 7,484

creating dummies (automatically)

You will have to post what auto3 initially looks like and be more specific about what you want to achieve.

The way your code is written, the twelve variables month_1-month_12 would have to already exist.  You may be looking for something like:

DATA auto3;

  set auto3;

  ARRAY month_ {12};

  DO i=1 TO 12;                                  

    month_(i) = ifn(i=month,1,0);

  END;

RUN;

Contributor
Posts: 44

creating dummies (automatically)

thnak you! you got it.

Respected Advisor
Posts: 3,799

creating dummies (automatically)

What if you don't know how many levels and/or you have many variables?

Valued Guide
Posts: 765

Re: creating dummies (automatically)

Hi ... maybe you can skip the loop and only change one value per observation (doesn't answer _null_'s question) ...

data auto3;

array month_(12) (12*0);

set auto3;

month_(month) = 1;

output;

month_(month) = 0;

run;

Respected Advisor
Posts: 3,799

Re: creating dummies (automatically)

The answer to my question is PROC TRANSREG or PROC GLMMOD. :smileyplain:

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 203 views
  • 0 likes
  • 4 in conversation