turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- What function do I need to create this indicator v...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-29-2014 02:33 PM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-29-2014 02:58 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-30-2014 01:42 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-30-2014 01:58 PM

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

data want;

set have;

newvar=0;

if sum(of v>=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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-29-2014 03:15 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-29-2014 08:52 PM

Sounds like you just need the SUM() function.

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-30-2014 12:06 PM

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);

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-30-2014 02:44 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-30-2014 05:37 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-30-2014 01:41 PM

Thank you! All these answers were so helpful.