BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
igbuzov
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
dcruik
Lapis Lazuli | Level 10

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;

PGStats
Opal | Level 21

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
slchen
Lapis Lazuli | Level 10

proc freq data=new;

table Nationality;

output out=temp;

run;

proc transpose data=temp(drop=percent) prefix=Number_of_ out=want(drop=_:);

id nationality;

run;

igbuzov
Calcite | Level 5

Thanks guys for your help 🙂

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 14668 views
  • 6 likes
  • 4 in conversation