BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mona4u
Lapis Lazuli | Level 10

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 ?? 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

12 REPLIES 12
Astounding
PROC Star

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.

ballardw
Super User

@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.

mona4u
Lapis Lazuli | Level 10

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
5004
5005
5006
5007
5008
5009
 
Astounding
PROC Star

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.

mona4u
Lapis Lazuli | Level 10
I need to count the number of observation in one variable not in the whole set
Astounding
PROC Star

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.

ballardw
Super User

@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.

mona4u
Lapis Lazuli | Level 10
I need the not missing also I cannot use proc means bc my variable has characteristics and numeric part as above.
Astounding
PROC Star

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;

MarkWik
Quartz | Level 8

@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.

mona4u
Lapis Lazuli | Level 10

if you don't want to help. 

please stay silent 

JBailey
Barite | Level 11

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 1299 views
  • 2 likes
  • 5 in conversation