Desktop productivity for business analysts and programmers

How to generate a code that will count rows by 3

Reply
N/A
Posts: 0

How to generate a code that will count rows by 3

Its fine now thanks. Message was edited by: HamlinChris
Trusted Advisor
Posts: 2,114

Re: How to generate a code that will count rows by 3

Sorry, your goal is unclear.

If your data are in triplets of rows, then you can read 3 rows at a time in the INPUT statement:

INPUT a b c / aa bb cc / aaa bbb ccc;

would read

a b c from the first row,
aa bb cc from the second, and
aaa bbb ccc from the third.
SAS Super FREQ
Posts: 8,819

Re: How to generate a code that will count rows by 3

Hi:
It is unclear to me what $rowcountx is. It almost looks like the kind of variable that you might use in TAGSET template code. Or maybe it's something else???

At any rate, if you already have data and you need to count out rows by thirds, you can use the MOD function to generate this:
[pre]
Use Mod Function

mod_ count_ grp_
Obs third third third Name Sex Age Height Weight

1 1 1 1 Alfred M 14 69.0 112.5
2 2 2 1 Alice F 13 56.5 84.0
3 0 3 1 Barbara F 13 65.3 98.0
4 1 1 2 Carol F 14 62.8 102.5
5 2 2 2 Henry M 14 63.5 102.5
6 0 3 2 James M 12 57.3 83.0
7 1 1 3 Jane F 12 59.8 84.5
8 2 2 3 Janet F 15 62.5 112.5
9 0 3 3 Jeffrey M 13 62.5 84.0
10 1 1 4 John M 12 59.0 99.5
11 2 2 4 Joyce F 11 51.3 50.5
12 0 3 4 Judy F 14 64.3 90.0
13 1 1 5 Louise F 12 56.3 77.0
14 2 2 5 Mary F 15 66.5 112.0
15 0 3 5 Philip M 16 72.0 150.0
16 1 1 6 Robert M 12 64.8 128.0
17 2 2 6 Ronald M 15 67.0 133.0
18 0 3 6 Thomas M 11 57.5 85.0
19 1 1 7 William M 15 66.5 112.0

[/pre]

Note that with the mod function, the values are either 1,2 or 0 if you have a statement like:
[pre]mod_third = mod(_n_,3); [/pre]

You can then use the mod_third variable to set other values, depending on whether you want to "count off" by 3s or whether you want to assign groups by 3s. Here's the program that produced the above output.

cynthia
[pre]

data count;
length mod_third count_third grp_third 8;
set sashelp.class;
retain grp_third;

mod_third = mod(_n_,3);
if _n_ = 1 then grp_third = 1;
else if _n_ gt 1 and mod_third = 1
then grp_third + 1;

count_third = mod_third;
if mod_third = 0 then count_third = 3;
run;

proc print data=count;
title "Use Mod Function";
run;
[/pre]
Ask a Question
Discussion stats
  • 2 replies
  • 102 views
  • 0 likes
  • 3 in conversation