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 is wrong with this array (and what is the cor...

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-24-2014 01:05 AM

I have some variables, let's say 5, dummy coded. I want to create a variable that returns 1 if any dummy variable is 1 and 0 if all dummy variables are 0. Here's the array I got:

data temp;set temp;

array dummy(5) var1-var5;

do i=1 to 5;

if dummy*=1 then newvar=1;*

else if dummy*=0 then newvar=0;*

else if dummy*=. then newvar=.;*

end;run;

The result looks wrong; well, in fact it's wrong. How can I make the right one?

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

Posted in reply to NonSleeper

04-24-2014 04:38 AM

You don't need a do loop, use this instead:

newvar=max(of var1-var5);

Data never sleeps

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

Posted in reply to NonSleeper

04-24-2014 05:06 AM

“ if any dummy variable is 1”

You mean all of variables are 1 or any one of them is 1 ?

if range(of var1-var5)=0 then do;

if var1=1 then new=1;

else if var1=0 then new=0;

end;

Xia Keshan

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

Posted in reply to Ksharp

04-24-2014 05:13 AM

I mean if any of the dummy variables is 1, then the newvar will return the value of 1. Does your program apply for this case?

Plus, it also means if all of dummy variables are not 1 (i.e., equal 0, as they are dummy), then the newvar will return the value of 0.

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

Posted in reply to NonSleeper

04-24-2014 05:24 AM

My code is for all of variables are 1 or all of variables are 0 .

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

Posted in reply to NonSleeper

04-24-2014 07:19 AM

Why not

newvar = MAX ( of var1 - var5) ;

?

Result is 1 if any are 1,

missing only if all are missing (as per OP)

otherwise 0

assuming var1 .. var5 can only have these values.

Richard