## Problems in creating dummy variables

Solved
Frequent Contributor
Posts: 130

# 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.

Accepted Solutions
Solution
‎04-12-2014 03:55 PM
Posts: 5,543

## 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

All Replies
Solution
‎04-12-2014 03:55 PM
Posts: 5,543

## 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
Posts: 8,127

## 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.(SAS 9.3)

Super User
Posts: 10,787

## 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 and locked.