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,....
Is this it?
%let n=11;
data want;
set have;
count + 1;
if count=&n+1 then count=1;
run;
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;
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
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;
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;
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.
Sorry, misunderstood then. Much simpler then..
%let n=11;
data want;
set sashelp.class;
do count=1 to &n.;
record=_N_;
output;
end;
run;
Is this it?
%let n=11;
data want;
set have;
count + 1;
if count=&n+1 then count=1;
run;
@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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.