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
- /
- Base SAS Programming
- /
- Why isn't my array working?

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

01-31-2017 05:22 AM - edited 01-31-2017 06:28 AM

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

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

Posted in reply to GKati

01-31-2017 06:35 AM - edited 01-31-2017 06:42 AM

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

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

Posted in reply to GKati

01-31-2017 05:27 AM

Hi, Change this line it will work,

rdummies[i]=nyc*dummies[i]

##- Please type your reply above this line. Simple formatting, no

attachments. -##

rdummies[i]=nyc*dummies[i]

##- Please type your reply above this line. Simple formatting, no

attachments. -##

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

Posted in reply to GKati

01-31-2017 05:29 AM - edited 01-31-2017 05:31 AM

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

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

Posted in reply to draycut

01-31-2017 05:32 AM - edited 01-31-2017 05:33 AM

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

Thanks again.

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

Posted in reply to GKati

01-31-2017 05:34 AM

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

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

Posted in reply to draycut

01-31-2017 05:42 AM

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

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

Posted in reply to GKati

01-31-2017 05:39 AM

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.

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

01-31-2017 05:55 AM

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.

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

Posted in reply to GKati

01-31-2017 06:04 AM

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.

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

01-31-2017 06:19 AM

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

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

Posted in reply to GKati

01-31-2017 06:35 AM - edited 01-31-2017 06:42 AM

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;

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

02-01-2017 02:57 PM

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.

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

Posted in reply to GKati

02-02-2017 04:13 AM

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

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

Posted in reply to GKati

01-31-2017 06:26 AM

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

Maxims of Maximally Efficient SAS Programmers

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

Posted in reply to KurtBremser

01-31-2017 06:29 AM

Yes, true. Thanks!