Alvin
Alvin

Reputation: 1

SAS studio / SQL, how to count and sum the columns and create a new column to store them?

Dataset

This is the data set, what I want to do is to count how many Yes in each row, like the first row has 3 Yes so in my new column called Product_Held will have "3".

Upvotes: 0

Views: 333

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

In SQL, you can use:

proc sql;
    select t.*,
           ( (case when bank_account = 'Yes' then 1 else 0 end) +
             (case when credit_card = 'Yes' then 1 else 0 end) +
             . . . 
           ) as num_yeses
    from t;

You can create a view using:

proc sql;
    create view <viewname> as
        select t.*,
               ( (case when bank_account = 'Yes' then 1 else 0 end) +
                 (case when credit_card = 'Yes' then 1 else 0 end) +
                 . . . 
               ) as num_yeses
        from t;

Upvotes: 0

Related Questions