BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

data s ;
input name $ 15. ;
cards ;
dfsdfdas
dfsdfasdf
sdfwtrw
ghjghj
fghfdh
etwrt
rtyurtuy
rtrt
rtrysrt
rtrrt
;

 

assign record numbers like below table

name

number

dfsdfdas

1

dfsdfasdf

2

sdfwtrw

3

ghjghj

4

fghfdh

5

etwrt

1

rtyurtuy

2

rtrt

3

rtrysrt

4

rtrrt

5

9 REPLIES 9
Cynthia_sas
Diamond | Level 26
Hi:
I don't understand the logic of why the numbers restart at 1 on row for "etwrt" -- isn't that row #6?
Cynthia
thanikondharish
Fluorite | Level 6
Half records sequence number then again half records start from one number
gamotte
Rhodochrosite | Level 12
data s ;
input name $ 15. ;

call symput("nobs",_N_);
cards ;
dfsdfdas
dfsdfasdf
sdfwtrw
ghjghj
fghfdh
etwrt
rtyurtuy
rtrt
rtrysrt
rtrrt
;
run;

data want;
set S;
number=mod(_N_-1,floor(&nobs./2))+1;
run;
gamotte
Rhodochrosite | Level 12
data s ;
input name $ 15. ;

number=mod(_N_-1,5)+1;
cards ;
dfsdfdas
dfsdfasdf
sdfwtrw
ghjghj
fghfdh
etwrt
rtyurtuy
rtrt
rtrysrt
rtrrt
;
run;
ballardw
Super User

What is the rule that makes the record with etwrt restart the numbering at 1?

 

It would be poor practice to call a variable with repeated values as you show a "record number". The typical meaning would be a unique identifier.

 

this accomplishes your explicit requirement but with out a rule may not work in a different context.

data work.junk ;
input name $ 15. ;
number= mod(_n_ -1,5)+1;
cards ;
dfsdfdas
dfsdfasdf
sdfwtrw
ghjghj
fghfdh
etwrt
rtyurtuy
rtrt
rtrysrt
rtrrt
;
Haikuo
Onyx | Level 15

The following code will gave you half/half, if it is odd number, then the first half gets one more (this can be easily tweaked if you want otherwise). Assume that you already have the data as SAS table:

data s ;
input name $ 15. ;
cards ;
dfsdfdas
dfsdfasdf
sdfwtrw
ghjghj
fghfdh
etwrt
rtyurtuy
rtrt
rtrysrt
rtrrt
dsafg
;


data want;
if _n_=1 then chunk=ceil(n/2);
retain chunk;
do rec=1 by 1 to chunk;
set s nobs=n;
output;
end;
drop chunk;
run;
novinosrin
Tourmaline | Level 20

Not sure, if chuck var is needed to be computed as a PDV host

 

data s ;
input name $ 15. ;
cards ;
dfsdfdas
dfsdfasdf
sdfwtrw
ghjghj
fghfdh
etwrt
rtyurtuy
rtrt
rtrysrt
rtrrt
dsafg
;
data want;
do rec= 1 to ceil(n/2);
set s nobs=n;
output;
end;
run;

Haikuo
Onyx | Level 15

It depends. The overhead cost of additional variable vs. 2 times of ceil() and n/2 is debatable, and in this case, ignorable, IMHO.

Ksharp
Super User
data s ;
input name $ 15. ;
cards ;
dfsdfdas
dfsdfasdf
sdfwtrw
ghjghj
fghfdh
etwrt
rtyurtuy
rtrt
rtrysrt
rtrrt
;

data want;
 set s;
 if mod(_n_,5)=1 then n=0;
 n+1;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2625 views
  • 2 likes
  • 7 in conversation