## Why isn't my array working?

Solved
Frequent Contributor
Posts: 107

# Why isn't my array working?

[ Edited ]

Hi SAS users,

I am trying to multiply two dummy variables together. I get zeros and missing values (.) but no '1', when I know that both dummies have values 0 and 1 and they SHOULD produce some '1'.

data old;

set new;
INPUT id dummy1 dummy2 nyc rdummy1 rdummy2;
DATALINES;
1 1 0 0 1 0
2 0 1 0 0 0
3 0 1 0 0 1
4 1 0 1 1 1
6 1 0 0 0 1
7 0 0 0 1 1
8 0 0 1 0 1
9 1 0 1 0 1
10 1 1 0 1 0
11 1 0 1 0 1

nyc=0;

if location="NEW YORK" then nyc=1;

array dummies [19] dummy1 - dummy19;

array rdummies [19] rdummy1 - rdummy19;

do i=1 to 19;

rdummies[i]=nyc*dummies[i];

output;

end;

run;

Accepted Solutions
Solution
‎02-01-2017 02:49 PM
Super User
Posts: 9,599

## Re: Why isn't my array working?

[ Edited ]

Thanks, so code like this works:

Editing now to show that exactly the same happens with a logic gate AND, hence why I questioned the value of creating x new variables, when locig would also result in the same:

data survey (drop=i);
input id dummy1 dummy2 nyc;
array dummy{2};
array rdummy{2} 8;
array logic{2} 8;
do i=1 to 2;
rdummy{i}=nyc * dummy{i};
logic{i}=ifn(nyc and dummy{i},1,0);
end;
datalines;
1 1 0 0
2 0 1 0
3 0 1 0
4 1 0 1
6 1 0 0
7 0 0 0
8 0 0 1
9 1 0 1
10 1 1 0
11 1 0 1
;
run;

All Replies
Occasional Contributor
Posts: 12

## Re: Why isn't my array working?

Hi, Change this line it will work,
rdummies[i]=nyc*dummies[i]

attachments. -##
PROC Star
Posts: 1,259

## Re: Why isn't my array working?

[ Edited ]

You are missing a [i] behind dummies in your loop

If that still does not work, I recommend that you post some sample data in the form of a data step

Frequent Contributor
Posts: 107

## Re: Why isn't my array working?

[ Edited ]

Sorry my message was incorrectly specified. Message edited. The question unfortuntelly remains....

Thanks again.

PROC Star
Posts: 1,259

## Re: Why isn't my array working?

So really what you want is a 2 dimensional array (19x19) with indicator variables?

Frequent Contributor
Posts: 107

## Re: Why isn't my array working?

I think so. That might be it. I want to multiply a series of dummy variables (dummy1- dummy19) with another dummy variable (nyc) and store the solution in a third variable (rdummy1-rdummy19).

Super User
Posts: 9,599

## Re: Why isn't my array working?

Another good example of why you should post test data in the form of a datastep.  What does dummies{x} contain?  I.e. what is the benefit of duplicating all 19 variables just to get a pos/neg value?  If they contain 0 or 1 as you say, then creating 19 more with 1*val is doing nothing other than creating 19 duplicates of what you already have.

Frequent Contributor
Posts: 107

## Re: Why isn't my array working?

That wasn't a very helpful comment. At the moment, I don't know how else I would do what I'm doing. I don't know what you mean by pos/negative values, there should only be 0's and 1's.

Super User
Posts: 9,599

## Re: Why isn't my array working?

You haven't shown us what you are doing.  I am merely guessing from what you have posted:

"I get zeros and missing values (.) but no '1', when I know that both dummies have values 0 and 1 and they SHOULD produce some '1'."

So I am assuimng from that you require binary values, either 0 or 1.  However we do not know what dummies{x} contains, you haven't supplied any information, so the first part of my question is what does it look like - post some test data - in the form of a datastep - per the guidance on good posting which you will see underneath Post when you post a new question.  This helps us see what data you have, what structure it is, and gives us something to work with.

The second part of my question is the why of it.  From what you post above, you expect a set of 19 variables with 1 or 0, and you multiple a value by 1, which indicates the values probably lie between 0 and 1 anyway, so the question is why duplicate the data you already have in a second set of varaibles.

Without knowing what you are doing or working with its very hard to provide a good answer.

Frequent Contributor
Posts: 107

## Re: Why isn't my array working?

Ok, now I understand. I edited the message with test data. The nyc variable is binary (has 0's and 1's).

Solution
‎02-01-2017 02:49 PM
Super User
Posts: 9,599

## Re: Why isn't my array working?

[ Edited ]

Thanks, so code like this works:

Editing now to show that exactly the same happens with a logic gate AND, hence why I questioned the value of creating x new variables, when locig would also result in the same:

data survey (drop=i);
input id dummy1 dummy2 nyc;
array dummy{2};
array rdummy{2} 8;
array logic{2} 8;
do i=1 to 2;
rdummy{i}=nyc * dummy{i};
logic{i}=ifn(nyc and dummy{i},1,0);
end;
datalines;
1 1 0 0
2 0 1 0
3 0 1 0
4 1 0 1
6 1 0 0
7 0 0 0
8 0 0 1
9 1 0 1
10 1 1 0
11 1 0 1
;
run;

Frequent Contributor
Posts: 107

## Re: Why isn't my array working?

Thank you the ifc() seems to work out well. Theoretically these three commands should produce the same results:

rdummy{i}=nyc * dummy{i};
if dummy{i}=1 & nyc=1 then rdummy{i}
logic{i}=ifn(nyc and dummy{i},1,0);

in a do-loop for i=1 to 19 (remember my dataset has 19 variables not only 2). But in practice the calculations are incomplete for the first two. SAS creates a variable i running from 1 to 19 and rdummy is only calculated for observations where i equals the index of the dummy. Somehow for the ifn() logic statement, the variable i is a constant number (20 in my case) so this problem doesn't occur. This is what I was trying to understand how this works.

Super User
Posts: 9,599

## Re: Why isn't my array working?

If your having difficulty with something, then you will need to post exact and complete test data which covers the issue.  Follow;

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

I can't tell anything from what you have posted about the data.  There are a couple of typos in what you have posted though:

rdummy{i}=nyc * dummy{i};    If nyc or dummy{i} is missing then multiplication wont work
if dummy{i}=1 & nyc=1 then rdummy{i} This doesn't do anything, no assignment, missing semicolon, you
also drop the =1 from both checks as if 1 is always true.
logic{i}=ifn(nyc and dummy{i},1,0);
Super User
Posts: 10,209

## Re: Why isn't my array working?

GKati wrote:

Hi SAS users,

I am trying to multiply two dummy variables together. I get zeros and missing values (.) but no '1', when I know that both dummies have values 0 and 1 and they SHOULD produce some '1'.

DATA survey;
INPUT id dummy1 dummy2 nyc rdummy1 rdummy2;
DATALINES;
1 1 0 0 1 0
2 0 1 0 0 0
3 0 1 0 0 1
4 1 0 1 1 1
6 1 0 0 0 1
7 0 0 0 1 1
8 0 0 1 0 1
9 1 0 1 0 1
10 1 1 0 1 0
11 1 0 1 0 1

data old;

set new;

nyc=0;

if location="NEW YORK" then nyc=1;

array dummies [19] dummy1 - dummy19;

array rdummies [19] rdummy1 - rdummy19;

do i=1 to 19;

rdummies[i]=nyc*dummies[i];

output;

end;

run;

Your code is absolutely inconsistent. You create dataset "survey", but never use it; instead you try to create dataset "old" from "new".

Even if you used "survey", it only has variables up to count 2, while your arrays span over 19 counts!

Please review those issues, and come back with your real code that you tried.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 107