## increment +1 after repeating 3 times

Solved
Regular Contributor
Posts: 209

# increment +1 after repeating 3 times

How would I construct the following variable?

The variable increments +1 after repeating the number 3 times.

1

1

1

2

2

2

3

3

3

4

4

4

.

.

.

Accepted Solutions
Solution
‎05-21-2012 06:44 PM
Posts: 3,167

## Re: increment +1 after repeating 3 times

Not sure I have completely understand what you mean, but is this what you want?

data have;

input v\$ @@;

cards;

a b c d e f g

;

data _null_;

nobs=nobs;

call symputx('n',nobs);

set have nobs=nobs;

stop;

run;

data make;

do i=1 to round(&n/3)+1;

do j=1 to 3;

output;

end;

end;

drop j;

run;

data want;

set have;

set make;

run;

proc print;run;

Haikuo

All Replies
Posts: 3,167

## Re: increment +1 after repeating 3 times

assuming your end value is 100:

data want;

do i=1 to 100;

do j=1 to 3;

output;

end;

end;

drop j;

run;

Haikuo

Regular Contributor
Posts: 209

## Re: increment +1 after repeating 3 times

Is it possible to add this code to an existing data set without each row of the data set repeating itself i*j times?

Solution
‎05-21-2012 06:44 PM
Posts: 3,167

## Re: increment +1 after repeating 3 times

Not sure I have completely understand what you mean, but is this what you want?

data have;

input v\$ @@;

cards;

a b c d e f g

;

data _null_;

nobs=nobs;

call symputx('n',nobs);

set have nobs=nobs;

stop;

run;

data make;

do i=1 to round(&n/3)+1;

do j=1 to 3;

output;

end;

end;

drop j;

run;

data want;

set have;

set make;

run;

proc print;run;

Haikuo

Posts: 3,167

## Re: increment +1 after repeating 3 times

Two of the above steps are possible to be combined into one:

data make;

do i=1 to round(&n/3)+1;

do j=1 to 3;

set have;

output;

end;

end;

drop j;

run;

Haikuo

Regular Contributor
Posts: 165

## Re: increment +1 after repeating 3 times

Another option in one datastep is:

data want;

set have;

if mod(_N_,3)=1 then val+1;

run;

Posts: 3,167

## Re: increment +1 after repeating 3 times

Obviously this is a better option! Thanks for sharing!

Regards,

Haikuo

🔒 This topic is solved and locked.