BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Saba1
Quartz | Level 8
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
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Assuming you want to add this column to existing data:

 

data want;
set have;
id  = ceil(_n_ / 5);
run;
PG

View solution in original post

8 REPLIES 8
r_behata
Barite | Level 11

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
Saba1
Quartz | Level 8
@r_behata: thanks. No, i want "1" to be in first five rows, then "2" in next 5 rows, and so on.
r_behata
Barite | Level 11
data want;
	do i=1 to 5;
		j=1;
		do while(j < 6);
			num=i;
			output;
			j+1;
		end;
	end;

	drop i j;
run;
yabwon
Onyx | Level 15
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



Astounding
PROC Star

Perhaps this is what you are looking for:

 

data want;
do num=1 to 5;
   output; output; output; output; output;
end;
run;
PGStats
Opal | Level 21

Assuming you want to add this column to existing data:

 

data want;
set have;
id  = ceil(_n_ / 5);
run;
PG
smantha
Lapis Lazuli | Level 10
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?

ballardw
Super User

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?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3189 views
  • 6 likes
  • 7 in conversation