## counting the variable rows

Solved
Regular Contributor
Posts: 158

# counting the variable rows

I have a variable and I need to count how many rows in that variable each time I run the program

I need a good way to do it ??

Accepted Solutions
Solution
‎02-15-2018 04:12 PM
Super User
Posts: 6,754

## Re: counting the variable rows

For practical purposes, this would do:

data _null_;

set have nobs=_nobs_;

put 'Number of rows is ' _nobs_;

stop;

run;

Technically, there are rare cases where this will get you the wrong answer and require a more complex program.  It's inconceivable that you would run into one of those rare cases.

All Replies
Super User
Posts: 6,754

## Re: counting the variable rows

As silly as it might seem, you have to give an example.  There are a couple of ways to interpret the result that you would like.

Super User
Posts: 13,508

## Re: counting the variable rows

mona4u wrote:

I have a variable and I need to count how many rows in that variable each time I run the program

I need a good way to do it ??

"how many rows in that variable" isn't very clear. Variables do no contain "rows" generally.

Details. Do you need to know the number observations in the data set, non-missing for a specific variable, non-missing for multiple variables and/or the number of different values for a variable and how many times they occur? And do you need this in a data set or report?

It really helps to provide a small example of data and what the result should be.

Regular Contributor
Posts: 158

## Re: counting the variable rows

this is the variable that  I have  and I need to know how many rows are in this variable from obs 1 which is 1000 to last one 5009

I need a function or do loop that can count the number of rows.

 Site Number 1000 1001 1050 1100 1200 1250 1252 1300 1350 1351 1400 1401 1450 1451 1452 1453 1500 1600 1601 1602 1700 1750 5000 5002 5003Main Campus 5003Burbank 5003Pasadena 5003Torrance 5003Valencia 5003Ventura 5003Westlake 5004 5005 5006 5007 5008 5009
Solution
‎02-15-2018 04:12 PM
Super User
Posts: 6,754

## Re: counting the variable rows

For practical purposes, this would do:

data _null_;

set have nobs=_nobs_;

put 'Number of rows is ' _nobs_;

stop;

run;

Technically, there are rare cases where this will get you the wrong answer and require a more complex program.  It's inconceivable that you would run into one of those rare cases.

Regular Contributor
Posts: 158

## Re: counting the variable rows

I need to count the number of observation in one variable not in the whole set
Super User
Posts: 6,754

## Re: counting the variable rows

The number of observations is constant.  It has no relationship to which variable or variables you look at.  If you don't want the number of observations in the entire data set, you will need to add more detail about what you want.

Super User
Posts: 13,508

## Re: counting the variable rows

mona4u wrote:
I need to count the number of observation in one variable not in the whole set

You still have clarified if you need the number of times your variable is not missing or just row count in the data set.

```data example;
input x;
datalines;
1
2
3
.
4
5
6
;
run;

Proc means data=example n nmiss;
var x;
run;```

There is one missing value for the variable x. So do you want the n value of 6 or 7 (which is the number of rows of the data set and has nothing whatever to do with the variable x) or something else.

Note: proc means won't work with character variables but there are approaches depending on what you actually want.

Regular Contributor
Posts: 158

## Re: counting the variable rows

I need the not missing also I cannot use proc means bc my variable has characteristics and numeric part as above.
Super User
Posts: 6,754

## Re: counting the variable rows

Since there's a small data set, and interest in just the one variable:

data _null_;

set have end=done;

where site_number > ' ';

if done then put 'Number of nonmissing rows is ' _n_;

run;

Frequent Contributor
Posts: 103

## Re: counting the variable rows

@mona4u Before jumping to the forum for help, you could google search basic options etc that has various sources with answers for your question. It's only fair to demonstrate your effort before asking very very fundamental questions although apparently some people do have the time to answer all of it no matter how simple it is.

Regular Contributor
Posts: 158

## Re: counting the variable rows

if you don't want to help.

SAS Employee
Posts: 279

## Re: counting the variable rows

Hi @mona4u

Might as well add some SQL examples to the mix...

``````data work.test;
input mynum 11.0;
datalines;
1
2
3
4
5
6
7
8
8
8
9
9
10
11

run;

/* Count the observations */
proc sql;
select count(*) from work.test;
quit;

/* Count the distinct values */
proc sql;
select count(*) from (select distinct mynum from work.test);
quit;

/* Distinct values with the number of times each occurs */
proc sql;
select mynum, count(*)
from work.test
group by mynum;
quit;
``````

Best wishes,

Jeff

☑ This topic is solved.