Problems in creating dummy variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 130
Accepted Solution

Problems in creating dummy variables

Here is what I wanna do:

create 7 dummy variables according to different time conditions. for example,

if year = 1994 1995 1996, then dummy_1 = 0;

if year = 1997 1998 1999, then dummy_1=1;

if other years, then dummy_1 =2.

and the year condition for dummy_2 is set by rolling one year forward, same for dummy_3 .... dummy_7.

Here is what I did (but the result is not what I expected, as showed in the attached picture):

First, create 7 dummy variables with missing values;

Second, replace those missing values according to different conditions.

*step 1: create  7 dummy varialbes with missing value;

data bbb (DROP = i);

  set period;

  array A {*} period_1 - period_7;

  do i = 1 to 7;

     A(i) = . ;

  end;

run;

*step 2: replace all missing values accoring to different time conditions;

%macro perioddummy;

%do count = 1 %to 7;

data ccc;

  set bbb;

  if (1993 + &count) <= year <= (1995 + &count) then period_&count=0;

  else if (1996 + &count) <= year <= (1998 + &count) then period_&count=1;

  else period_&count=2;

run;

%end;

%mend;

%perioddummy;


Would you please help to correct the above code, or, just ignore above code and suggest a better solution? Really appreciate your valuable time.


show results.png
Attachment

Accepted Solutions
Solution
‎04-12-2014 03:55 PM
Respected Advisor
Posts: 4,654

Re: Problems in creating dummy variables

Try this:

data want;

set sasforum.period;

array p{*} period_1-period_7;

do i = 1 to dim(p);

     select (floor((year-1993-i)/3));

          when (0)   p{i} = 0;

          when (1)   p{i} = 1;

          otherwise p{i} = 2;

          end;

     end;

output;

drop i;

run;

proc print data=want noobs; run;

PG

PG

View solution in original post


All Replies
Solution
‎04-12-2014 03:55 PM
Respected Advisor
Posts: 4,654

Re: Problems in creating dummy variables

Try this:

data want;

set sasforum.period;

array p{*} period_1-period_7;

do i = 1 to dim(p);

     select (floor((year-1993-i)/3));

          when (0)   p{i} = 0;

          when (1)   p{i} = 1;

          otherwise p{i} = 2;

          end;

     end;

output;

drop i;

run;

proc print data=want noobs; run;

PG

PG
Frequent Contributor
Posts: 130

Re: Problems in creating dummy variables

Many thanks, PG! That is elegant and efficient!

Super User
Super User
Posts: 6,502

Re: Problems in creating dummy variables

You can replace:

  select (floor((year-1993-i)/3));

          when (0)   p{i} = 0;

          when (1)   p{i} = 1;

          otherwise p{i} = 2;

   end;

With

  p{i}= min(2,(floor((year-1993-i)/3)));


Frequent Contributor
Posts: 130

Re: Problems in creating dummy variables

Thanks, Tom! that also works well, except that values for "otherwise" will not only equal to 2, but also -1 and -2.

Since the years I care about are those with dummy variables equal to 0 or 1, I can use " p{i}= min(2,(floor((year-1993-i)/3)))" for my program.new result.png(SAS 9.3)

Super User
Posts: 9,687

Re: Problems in creating dummy variables

libname x v9 'c:\temp\';
data want;
 set x.period;
array p{*} period_1-period_7;
do i = 1 to dim(p);
 x=year+1-i;
 if x in (1994,1995,1996 ) then p{i}=0;
  else if x in (1997,1998,1999 ) then p{i}=1;
   else p{i}=2;
end;
drop i x;
run;

Xia Keshan

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 530 views
  • 3 likes
  • 4 in conversation