Help using Base SAS procedures

how to count number of observations

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

how to count number of observations

hello

I have this dataset:

data new;

        infile datalines;

        input ID $ Name $ Nationality $ Price;

datalines;

01    Hans     Y    130

02    Pierre    Y    100

03    Marko    N    80

04    Ivan       N     95

05    John      Y    120

06    Ana       N    75

07    Petra     N    45

;

I would like, as result to have new dataset with two new variables which would count the number of Nationality(Y) and Nationality(N), something like this

proc print data=result;

run;

    obs   Number_of_Y    Number_of_N

     1              3                    4

I got this by using proc sql, created two views, based on them i created new tabel named result. I would like to know how to do this using data step programming or some better way using proc sql queries.

Thank you


Accepted Solutions
Solution
‎06-18-2015 04:14 PM
Respected Advisor
Posts: 4,920

Re: how to count number of observations

Alternative SQL query :

proc sql;

select

    sum(nationality="Y") as Number_of_Y,

    sum(nationality="N") as Number_of_N

from new;

quit;

PG

PG

View solution in original post


All Replies
Frequent Contributor
Posts: 130

Re: how to count number of observations

Here's a way to do it with PROC SQL, not sure if you want the "obs" field in your result data set or not.  Hope this is what you're looking for!

data have;

input ID $ Name $ Nationality $ Price;

datalines;

01 Hans Y 130

02 Pierre Y 100

03 Marko N 80

04 Ivan N 95

05 John Y 120

06 Ana N 75

07 Petra N 45

;

run;

proc sql;

create table want as

select 1 as obs,

     sum(case when Nationality="Y" then 1

          Else 0 end) as Number_of_Y,

     sum(case when Nationality="N" then 1

          Else 0 end) as Number_of_N

from have;

quit;

Solution
‎06-18-2015 04:14 PM
Respected Advisor
Posts: 4,920

Re: how to count number of observations

Alternative SQL query :

proc sql;

select

    sum(nationality="Y") as Number_of_Y,

    sum(nationality="N") as Number_of_N

from new;

quit;

PG

PG
Super Contributor
Posts: 275

Re: how to count number of observations

proc freq data=new;

table Nationality;

output out=temp;

run;

proc transpose data=temp(drop=percent) prefix=Number_of_ out=want(drop=_Smiley Happy;

id nationality;

run;

New Contributor
Posts: 3

Re: how to count number of observations

Thanks guys for your help :-)

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 4772 views
  • 6 likes
  • 4 in conversation