DATA Step, Macro, Functions and more

Count of observations with at least one condition met

Accepted Solution Solved
Reply
Occasional Learner
Posts: 1
Accepted Solution

Count of observations with at least one condition met

[ Edited ]

 

I have a dataset that looks like the following:

 

ID      MONTH      VAR

1       1                 2

1       2                 2

1       3                 1

2       1                 2

2       2                 2

2       3                 2 

3       1                 1

3       2                 1 

3       3                 1 

 

I need to get a count of ID's where VAR=1 for at least one of the months.

I'm not a very advanced user.

Is there code that could easily accomplish what I'm looking for?


Accepted Solutions
Solution
‎12-20-2016 03:13 PM
Super User
Posts: 5,497

Re: Count of observations with at least one condition met

This might be the easiest way:

 

proc sql;

select count(distinct id) from your_dataset where var=1;

quit;

 

View solution in original post


All Replies
Frequent Contributor
Posts: 136

Re: Count of observations with at least one condition met

[ Edited ]

If you need just the count then use PROC SQL.

 

data one;

infile datalines;

input ID MONTH VAR;

datalines;

1 1 2

1 2 2

1 3 1

2 1 2

2 2 2

2 3 2

3 1 1

3 2 1

3 3 1

;

run;

 

proc sql;

select distinct count(id) INTO: count

from one

where var=1 and month=1;

quit;

Thanks,
Suryakiran
Solution
‎12-20-2016 03:13 PM
Super User
Posts: 5,497

Re: Count of observations with at least one condition met

This might be the easiest way:

 

proc sql;

select count(distinct id) from your_dataset where var=1;

quit;

 

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 357 views
  • 1 like
  • 3 in conversation