DATA Step, Macro, Functions and more

Condition based counting

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Condition based counting

Hi everyone,

 

I'm trying to count the number of females in each country and group it by country in my data set. I've posted my code thus far but am not getting far. 

 

proc sql;
create table gender_dispurtion as
select gender, country,
	if gender = 'F' then
		count(*) label = '#_females'
	end
from customer_1
group by country
order by country;
quit;

38 if gender = 'F' then
------ -
1 22
200
WARNING 1-322: Assuming the symbol GE was misspelled as gender.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,
a numeric constant, a datetime constant, a missing value, (, +, -, ALL, ANY, BTRIM,
CALCULATED, CASE, INPUT, PUT, SELECT, SOME, SUBSTRING, TRANSLATE, USER.

ERROR 200-322: The symbol is not recognized and will be ignored.

39 count(*) label = '#_females'
40 end
---
22
ERROR 22-322: Syntax error, expecting one of the following: ',', AS.

 

I've attached my data set in excel because it does not allow me to attach SAS data tables for some reason.

 

Any help would be appreciated.

 

Thanks


Accepted Solutions
Solution
‎04-08-2017 11:29 PM
PROC Star
Posts: 7,492

Re: Condition based counting

proc sql doesn't support if then statements. I think you are looking for something like (untested):

 

proc sql;
  create table gender_dispurtion as
    select  country, count(*) as females
      from customer_1 (where=(gender eq 'F'))
        group by country
          order by country
  ;
quit;

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Solution
‎04-08-2017 11:29 PM
PROC Star
Posts: 7,492

Re: Condition based counting

proc sql doesn't support if then statements. I think you are looking for something like (untested):

 

proc sql;
  create table gender_dispurtion as
    select  country, count(*) as females
      from customer_1 (where=(gender eq 'F'))
        group by country
          order by country
  ;
quit;

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 19

Re: Condition based counting

Thanks everyone Smiley Happy

Contributor
Posts: 57

Re: Condition based counting

proc sql;
create table gender_dispurtion as
Select count(gender),country from customer_1 where upcase(gender) eq 'F'
group by country;
quit;
Super User
Posts: 19,869

Re: Condition based counting

I would suggest PROC FREQ

 

PROC FREQ data = customer_1;

table country*gender  / out= summary_by_gender;

run;
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 266 views
  • 3 likes
  • 4 in conversation