🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 06-15-2020 02:45 PM
(4481 views)
Hi
I want to create a sequence of unique numbers in a column, but each number must repeat 5 times (i.e. same number in 5 rows). For example number 1 in first five rows, then number 2 must again repeat five times and so on.
Please help. Thanks
I want to create a sequence of unique numbers in a column, but each number must repeat 5 times (i.e. same number in 5 rows). For example number 1 in first five rows, then number 2 must again repeat five times and so on.
Please help. Thanks
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Assuming you want to add this column to existing data:
data want;
set have;
id = ceil(_n_ / 5);
run;
PG
8 REPLIES 8
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is this what you are after ?
data want;
do i=1 to 5;
num=input(compress(repeat(i,5)),8.);
putlog num=;
output;
end;
drop i;
run;
Log : num=111111 num=222222 num=333333 num=444444 num=555555
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@r_behata: thanks. No, i want "1" to be in first five rows, then "2" in next 5 rows, and so on.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want;
do i=1 to 5;
j=1;
do while(j < 6);
num=i;
output;
j+1;
end;
end;
drop i j;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want;
do number = 1 to 42;
do _N_ = 1 to 5;
output;
end;
end;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Perhaps this is what you are looking for:
data want;
do num=1 to 5;
output; output; output; output; output;
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Assuming you want to add this column to existing data:
data want;
set have;
id = ceil(_n_ / 5);
run;
PG
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data new;
set old;
string=repeat(strip(put(int(_n_/5) +1,$32.)),5);
if mod(_n_,5) ==0 then string=repeat(strip(put(int(_n_/5) ,$32.)),5);
run;
Is this what you want?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
In pedantic mode I would say that you have conflicting requirements: "unique number" and "five times". Unique means one value.
And how many times is "so on"? How do we know when to quit? Are you attempting to add this value to an existing data set? Build a data set that only has this sequence?