BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GKati
Pyrite | Level 9

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

 

View solution in original post

14 REPLIES 14
abhaykumar
Obsidian | Level 7
Hi, Change this line it will work,
rdummies[i]=nyc*dummies[i]

##- Please type your reply above this line. Simple formatting, no
attachments. -##
PeterClemmensen
Tourmaline | Level 20

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 

GKati
Pyrite | Level 9

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

 

Thanks again.

PeterClemmensen
Tourmaline | Level 20

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

GKati
Pyrite | Level 9

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

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

GKati
Pyrite | Level 9

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

GKati
Pyrite | Level 9

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

 

GKati
Pyrite | Level 9

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. 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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);
Kurt_Bremser
Super User

@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.

GKati
Pyrite | Level 9

Yes, true. Thanks!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 1545 views
  • 0 likes
  • 5 in conversation