## Use of do loop to replicate observations and then assign default indicator

Solved
Occasional Contributor
Posts: 18

# Use of do loop to replicate observations and then assign default indicator

Hi all,

I have the data set provided below:-

Data have;

input start end score;

Datalines;

1 0 0.22

1 1 0.58

2 2 0.89

6 3 0.36

3 2 0.77

1 0 0.89

1 1 0.89

;

run;

“start” variable shows number of observations at start of period, while “end” shows the number of default observations at the end of period. With given data/scenario I have to create default indicator with 1 as default and 0 as not default. For example observation number 4 has value 6 for start variable and it has 3 defaults within the end variable so I need to replicate that observation 6 times i.e. 3 times with value 1 in the default indicator(“def_indi”) and 3 times with value 0.

Through my code I find myself successful in replicating the observation but not in creating the default indicator as desired. The desired data “want” is provided below.

My partial code:-

data want;

set have;

do i= 1 to (start);

def_indi=1;

output;

end;

run;

Data want;

input start end score Def_indi;

Datalines;

1 0 0.22 0

1 1 0.58 1

2 2 0.89 1

2 2 0.89 1

6 3 0.36 0

6 3 0.36 0

6 3 0.36 0

6 3 0.36 1

6 3 0.36 1

6 3 0.36 1

3 2 0.77 0

3 2 0.77 1

3 2 0.77 1

1 0 0.89 0

1 1 0.89 1

;

run;

I need it urgently so I would be highly thankful if someone could help me with it.

Tony

Accepted Solutions
Solution
‎05-03-2013 12:47 AM
Super User
Posts: 10,784

## Re: Use of do loop to replicate observations and then assign default indicator

OK.

```Data have;
input start end score;
Datalines;
1 0 0.22
1 1 0.58
2 2 0.89
6 3 0.36
3 2 0.77
1 0 0.89
1 1 0.89
;
run;
data want;
set have;
do i=1 to start-end;
Def_indi=0;
output;
end;
do i=1 to end;
Def_indi=1;
output;
end;
drop i;
run;

```

Ksharp

All Replies
Solution
‎05-03-2013 12:47 AM
Super User
Posts: 10,784

## Re: Use of do loop to replicate observations and then assign default indicator

OK.

```Data have;
input start end score;
Datalines;
1 0 0.22
1 1 0.58
2 2 0.89
6 3 0.36
3 2 0.77
1 0 0.89
1 1 0.89
;
run;
data want;
set have;
do i=1 to start-end;
Def_indi=0;
output;
end;
do i=1 to end;
Def_indi=1;
output;
end;
drop i;
run;

```

Ksharp

Not applicable
Posts: 1

## Re: Use of do loop to replicate observations and then assign default indicator

data want1 (drop=i);

set have;

do i=1 to start;

_default_=0;

if i<=end then do;

_default_=1;

end;

output;

end;

run;

🔒 This topic is solved and locked.