BookmarkSubscribeRSS Feed
gtucke1
Fluorite | Level 6

I have 11 variables with the following categories:

0 = absent

1 = active

2 = inactive

8 = uk/dk

9 = missing

 

I want to create a new variable for the total score for number of "1's".

 

Then I want to know how many study participants have 2 or more #1's.

 

I'm a new user and don't know if I need to do if then statements, sum statement etc. 

Appreciate any help.

 

 

 

 

 

8 REPLIES 8
Kurt_Bremser
Super User

You can use a data step with an array:

array vars {*} /* put your 11 variables here */;
count = 0;
do i = 1 to dim(vars);
  count + (vars{i} = 1);
end;

I would prefer a long data structure, which allows this simple code:

proc sql;
create table want as
  select
    id,
    sum(var = 1) as count
  from have
  group by id
;
quit;

Or

proc means data=have (where=(var = 1)) nway;
class id;
var var;
output out=want sum()=;
run;

 

gtucke1
Fluorite | Level 6

Hi,

 

Not sure how to input my information. In the examples you provided. 

The 11 variables are listed as dx_chf dx_copd dx_hiv etc. I want to add up all of the "active" which = 1 to come to a total of X active diagnoses. Then I want to determine how many participants have 2 or more active diagnoses. 

 

Thank you for the help.

sbxkoenk
SAS Super FREQ

Hello,

 

Something like this?

data have;
 input particip $ catvar1 $ catvar2 $ catvar3 $ catvar4  $ catvar5  $ 
       catvar6  $ catvar7 $ catvar8 $ catvar9 $ catvar10 $ catvar11 $;
datalines;
ABC 0 1 2 8 9 0 1 2 8 9 2
;
run;

data want(drop = i);
 set have;
 array catvar[11] $ catvar1 - catvar11;
 number_of_1s = 0;
 do i = 1 to dim(catvar);
  if catvar(i)='1' then number_of_1s = number_of_1s + 1;
 end;
run;

PROC FREQ data=want;
 tables number_of_1s ;
run;
/* end of program */

Koen

gtucke1
Fluorite | Level 6

Thank you for the help. I'm using a dataset, not data lines. Would I do something similar?

 

sbxkoenk
SAS Super FREQ

Hello @gtucke1 ,

 

This simply means you already have your data set 'have'.

In the 2nd data step (with the array processing), replace the data set named 'have' in the set statement by your own input data set.

I assumed that these 11 variables are string-type (char). You need slight changes if your 11 variables are numeric.

 

Koen

Tom
Super User Tom
Super User

Here is way to do it quickly that takes advantage of the fact that all of the possible values are one digit long.

Let's assume you have a dataset name HAVE and the variables are named VAR1 to VAR11.

data want;
  set have;
  score = countc(cats(of var1-var11),'1');
  over2 = (score > 1);
run;
proc freq data=want;
  tables over2;
run;

The CATS() function will create a string by concatenating the values of the listed variables with leading/trailing spaces removed.  If the any of the variables are numeric they will be silently converted to strings first.

The COUNTC() function counts how many times the characters in the second argument appear in the first argument. 

So SCORE will have how many 1's there were in the eleven variables.

SAS will convert a boolean expression like, score > 1, into 0 for false and 1 for true.  So OVER2 will be 1 when there were two or more 1's in the eleven varaibles.

PROC FREQ will show you the counts of 1 and 0's in the OVER2 variable.

gtucke1
Fluorite | Level 6

Thank you very much for the explanation. It worked! 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 546 views
  • 0 likes
  • 4 in conversation