increment +1 after repeating 3 times

Accepted Solution Solved
Reply
Regular Contributor
Posts: 199
Accepted Solution

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
Respected Advisor
Posts: 3,156

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

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

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: 199

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
Respected Advisor
Posts: 3,156

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

Respected Advisor
Posts: 3,156

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;

Respected Advisor
Posts: 3,156

Re: increment +1 after repeating 3 times

Obviously this is a better option! Thanks for sharing!

Regards,

Haikuo

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 527 views
  • 0 likes
  • 3 in conversation