What function do I need to create this indicator variable

Reply
Occasional Contributor ajb
Occasional Contributor
Posts: 18

What function do I need to create this indicator variable

Hi,

I have16 variables ID, v1-v15  where v1-v15 are coded 0 - no event and 1- event and ID is patient ID.

There are 100 obs.

what I want to be able to do in SAS is:

       if an observation = 1  for at least any 2  of the 15 indicator  variables then newvar=1

else  if an observation =0  less than any 2 of the 15 indicator variables then newvar=0

Any suggestions on how to do this?

Trusted Advisor
Posts: 1,228

Re: What function do I need to create this indicator variable

proc sql;

  select  NAME

  into    :v_list separated by ' '

  from    SASHELP.VCOLUMN

  where   LIBNAME="WORK"

  and memname="HAVE"

  and name like 'v%';

quit;

data want;

set have;

newvar=0;

if sum(of &v_list)>=2 then newvar=1;

run;

Occasional Contributor ajb
Occasional Contributor
Posts: 18

Re: What function do I need to create this indicator variable

I am not familiar with proc sql. Do you know of any useful resources where I could learn more about it?

Trusted Advisor
Posts: 1,228

Re: What function do I need to create this indicator variable

If you want to skip proc sql you can try this.

data want;

set have;

newvar=0;

if sum(of vSmiley Happy>=2 then newvar=1;

run;

This may be a good source for proc sql;

http://support.sas.com/documentation/cdl/en/sqlproc/63043/PDF/default/sqlproc.pdf

Super User
Posts: 19,167

Re: What function do I need to create this indicator variable

Use an array to reference your variables and then check if the sum of the variables is >2.

data want;

set have;

array v(15) v1-v15;

if sum(of v(*)) >2 then new_var=1;

else new_var=0;

run;

Super User
Super User
Posts: 6,845

Re: What function do I need to create this indicator variable

Sounds like you just need the SUM() function.

indicator = 1 < sum(of v1-v15) ;

Super User
Posts: 11,134

Re: What function do I need to create this indicator variable

A minor addition to consider with Tom's suggestion is IF you have any records with all of V1 to V15 missing do you want a 0? If so no problem. If however it should return a missing value for your indicator: If you never have all 15 missing then that's good to go also.

If nmiss(of v1-v15) <15 then indicator = 1<sum(of v1-v15);

Occasional Contributor
Posts: 17

Re: What function do I need to create this indicator variable

,

Maybe I am missing something obvious, which happens to me very offten,  but in :

indicator = 1 < sum(of v1-v15) ;

The indicator will never be missing, it is a results of boolean operation, so it will be either 0 or 1, and can't be anything else, including missing value. So pre-check the missing status of v1-v15 seems redundant.

while if the purpose is to make sure sum(of v1-v15) not missing, when it is, assign 0 to it, then 0 can be added into the equation: sum(of v1-v15,0)

my2cents,

Haikuo

Super User
Posts: 11,134

Re: What function do I need to create this indicator variable

My comment was a gentle reminder that 1) we don't have all of the data and 2) this is a subtle issue with Boolean assignment as all missing V1-V15 would have a sum of missing and 1<missing is false with a 0 assignment to indicator but that MAY not have been the intended result if there were all missing values for the V1-V15.

One potential issue is if the indicator is to be used in a regression model. Having an actual value of 0 if not intended for all missing values of V1-V15 could result in some odd results, especially if a frequent occurrence.

I use these types of recodes frequently have to ensure that I have a missing for the recode with all of the components are missing in most usages.

Occasional Contributor ajb
Occasional Contributor
Posts: 18

Re: What function do I need to create this indicator variable

Thank you! All these answers were so helpful.

Ask a Question
Discussion stats
  • 9 replies
  • 317 views
  • 6 likes
  • 6 in conversation