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 |
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;
data s ;
input name $ 15. ;
number=mod(_N_-1,5)+1;
cards ;
dfsdfdas
dfsdfasdf
sdfwtrw
ghjghj
fghfdh
etwrt
rtyurtuy
rtrt
rtrysrt
rtrrt
;
run;
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 ;
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;
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;
It depends. The overhead cost of additional variable vs. 2 times of ceil() and n/2 is debatable, and in this case, ignorable, IMHO.
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.