how to count the number of observations that meet certain conditions

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

how to count the number of observations that meet certain conditions

I always want to count the number of observations/list these observations that meet certain conditions I define. But I don't really know how to do it. For example, I have the following dataset. I want to find # of observations that firm=1 & dummy=B & assets=10, and list those observations. This seems to be easy as a short dataset, but if there are more than 1 million observations, it will be much more difficult. So I wonder if there is any procedure or other command that will run this purpose.

firm     date     dummy$     assets   

1          1          B               100   

1          1          S               10    

1          2          S               10

....


Accepted Solutions
Solution
‎10-31-2013 12:21 AM
Respected Advisor
Posts: 3,837

Re: how to count the number of observations that meet certain conditions

You could use quite a few SAS Procedures for this (Proc Means/Summary, Proc Report, Proc Tabulate,....).

Here some code using Proc SQL:

proc sql;

  select count(*) as N_Obs

  from have

  where firm=1 and dummy='B' and assets=10

  ;

quit;

View solution in original post


All Replies
Solution
‎10-31-2013 12:21 AM
Respected Advisor
Posts: 3,837

Re: how to count the number of observations that meet certain conditions

You could use quite a few SAS Procedures for this (Proc Means/Summary, Proc Report, Proc Tabulate,....).

Here some code using Proc SQL:

proc sql;

  select count(*) as N_Obs

  from have

  where firm=1 and dummy='B' and assets=10

  ;

quit;

Frequent Contributor
Posts: 122

Re: how to count the number of observations that meet certain conditions

Thank you. But is it possible to list what are they? And what (*) means here?

Respected Advisor
Posts: 3,837

Re: how to count the number of observations that meet certain conditions

If you have SAS EG then I suggest you use the Query Builder Wizard and "play around" with it until you get the result you want. After that look at the code generated by the wizard and eventually use the SAS Online Doc (Proc SQL) to understand the syntax.

Your questions are very basic so I feel you probably should first invest a bit of time in learning SAS and SQL language.

Frequent Contributor
Posts: 122

Re: how to count the number of observations that meet certain conditions

Thank you

Occasional Contributor
Posts: 16

Re: how to count the number of observations that meet certain conditions

Patrik, 

Thank you for this solution.  If I could, I wish to know how to assign N_Obs to a macro var such as &nobs or to a universal variable. Is that somthing that can be done within the proc sql?

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 6793 views
  • 3 likes
  • 3 in conversation