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;
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;
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
Sorry my message was incorrectly specified. Message edited. The question unfortuntelly remains....
Thanks again.
So really what you want is a 2 dimensional array (19x19) with indicator variables?
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).
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.
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.
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.
Ok, now I understand. I edited the message with test data. The nyc variable is binary (has 0's and 1's).
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;
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.
If your having difficulty with something, then you will need to post exact and complete test data which covers the issue. Follow;
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);
@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.
Yes, true. Thanks!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.