DATA Step, Macro, Functions and more

Enumerative Count Loop

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Enumerative Count Loop

Hi - I'm trying to create a variable that simply counts to an integer n, then repeats.

1,2,...,n-1,n,1,2,...n-1,n,...,1.2,....


Accepted Solutions
Solution
‎01-23-2018 12:00 PM
Super User
Posts: 6,626

Re: Enumerative Count Loop

Is this it?

 

%let n=11;

 

data want;

set have;

count + 1;

if count=&n+1 then count=1;

run;

View solution in original post


All Replies
PROC Star
Posts: 1,209

Re: Enumerative Count Loop

Something like this?

 

%let iter=3;
%let n=10;

data want(keep=x);
   do i=1 to &iter.;
      do x=1 to &n.;
         output;
      end;
   end;
run;
New Contributor
Posts: 4

Re: Enumerative Count Loop

Thanks so much. I have something similar...probably should have given more info in my question. Here's what I currently have.

%LET n = 11;

 

DATA test(DROP = i);

SET accts;

Do i=1 TO (&n.);

count=0+i;

OUTPUT;

END;

RUN;

 

For every original record this is creating 11 identical records with the addition of a new 'count' variable counting to 11. I'm looking for something to return the same number of observations with one new variable counting to n, then repeating.

 

I'm trying to get something like this

 

Record   Count 

1        1

2        2

3        3

..........

n        n

n+1      1

n+2      2

..........

n+n      n

n+n+1    1

n+n+2    2

 

And so on. Make sense?

 

Thanks in advance

 

 

 

PROC Star
Posts: 1,209

Re: Enumerative Count Loop

Something like this then?

 

%let n=11;

data want;
   set sashelp.class;
   do count=1 to &n.;
      output;
   end;
run;

data want;
   set want;
   record=_n_;
run;
PROC Star
Posts: 1,209

Re: Enumerative Count Loop

Come to think of it, this is simpler

 

%let n=11;

data want;
   set sashelp.class;
   do count=1 to &n.;
      record+1;
      output;
   end;
   retain record;
run;
New Contributor
Posts: 4

Re: Enumerative Count Loop

I'm getting the same results. Just to clarify...Here's what I'm getting.

Record      Count     

1                1

1                2

1                3

1                n

2                1

2                2

2                3

2                n

 

 

vs where I'm wanting to get to

 

Record   Count 

1        1

2        2

3        3

..........

n        n

n+1      1

n+2      2

 

Thanks for your help.

PROC Star
Posts: 1,209

Re: Enumerative Count Loop

[ Edited ]

Sorry, misunderstood then. Much simpler then..

 

%let n=11;

data want;
   set sashelp.class;
   do count=1 to &n.;
      record=_N_;
      output;
   end;
run;
Solution
‎01-23-2018 12:00 PM
Super User
Posts: 6,626

Re: Enumerative Count Loop

Is this it?

 

%let n=11;

 

data want;

set have;

count + 1;

if count=&n+1 then count=1;

run;

Super User
Super User
Posts: 7,932

Re: Enumerative Count Loop


dber wrote:

... 

I'm trying to get something like this

 

Record   Count 

1        1

2        2

3        3

..........

n        n

n+1      1

n+2      2

..........

n+n      n

n+n+1    1

n+n+2    2

 

And so on. Make sense?

Thanks in advance

 


You could just put a SET and OUTPUT statement inside the DO loop.

data want ;
  do n=1 to 11 ;
    set have;
    output;
  end;
run;

Or you could retain a new counter and use logic like IF/THEN or MOD to wrap the counter back to 1.

data want ;
  set have ;
  n+1;
  if n>11 then n=1;
run;
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 178 views
  • 0 likes
  • 4 in conversation