BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dber
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

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;
dber
Calcite | Level 5

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

 

 

 

PeterClemmensen
Tourmaline | Level 20

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;
PeterClemmensen
Tourmaline | Level 20

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;
dber
Calcite | Level 5

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.

PeterClemmensen
Tourmaline | Level 20

Sorry, misunderstood then. Much simpler then..

 

%let n=11;

data want;
   set sashelp.class;
   do count=1 to &n.;
      record=_N_;
      output;
   end;
run;
Astounding
PROC Star

Is this it?

 

%let n=11;

 

data want;

set have;

count + 1;

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

run;

Tom
Super User Tom
Super User

@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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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