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 ??
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.
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.
@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.
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 |
5003 Main Campus |
5003 Burbank |
5003 Pasadena |
5003 Torrance |
5003 Valencia |
5003 Ventura |
5003 Westlake |
5005 |
5006 |
5007 |
5008 |
5009 |
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.
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.
@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.
Please see this example
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.
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;
@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.
if you don't want to help.
please stay silent
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.