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
- /
- Arrays

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

08-23-2016 12:10 AM

Hello,

I am trying to perform two array steps for two variable groups in the same dataset **and then merge the resultant datasets obtained**

Is there an **easy/better way than the one shown below**?

data array1;

set have;

array Bx{8} Abx1-Abx8;

do i= 1 to 8;

if Bx{i} ne " " then new_X="Y";

end;

drop i;

run;

data array2;

set have;

array BY{5} NAby1-NAby5;

do i= 1 to 5;

if BY{i} ne " " then new_Y="Y";

end;

drop i;

run;

Thanks

Accepted Solutions

Solution

08-23-2016
01:32 AM

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

Posted in reply to robertrao

08-23-2016 12:28 AM

if cmiss(of Bx{*}) ne 8 then new_X="Y"; if cmiss(of By{*}) ne 5 then new_Y="Y";

All Replies

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

Posted in reply to robertrao

08-23-2016 12:24 AM

Use CMISS() : data array1; set have; array Bx{8} Abx1-Abx8; array BY{5} NAby1-NAby5; if cmiss(of Bx{*})=8 then new_X="Y"; if cmiss(of By{*})=5 then new_Y="Y"; run;

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

Posted in reply to Ksharp

08-23-2016 01:34 AM

Just to make sure , this code gives a "Y" if all the values are missing right????

Thanks

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

Posted in reply to robertrao

08-23-2016 01:36 AM

Wrong.

It matches your requested logic, if any variable in the list is not missing then it is set to Y.

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

Posted in reply to Reeza

08-23-2016 01:49 AM

Hello,

I meant this one does not suits my logic because its putting "Y" when all the values of the array are missing!!!

Am i right??

if cmiss(of Bx{*}) = 8 then new_X="Y";

if cmiss(of By{*}) = 5 then new_Y="Y";

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

Posted in reply to robertrao

08-23-2016 02:00 AM

That's why the code was Not Equals.

Im not sure what your trying to do anymore. As long as you test your code thoroughly you should be fine.

Solution

08-23-2016
01:32 AM

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

Posted in reply to robertrao

08-23-2016 12:28 AM

if cmiss(of Bx{*}) ne 8 then new_X="Y"; if cmiss(of By{*}) ne 5 then new_Y="Y";

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

Posted in reply to Ksharp

08-23-2016 01:25 AM

Hello Ksharp,

I think I am a bit confused now.

The logic I wanted is that if any of the 8 positions in a row (atleast 1 position) is not missng then we give a "Y".

Same holds for the second array..If any of the 5 positions in a row is non missing we give a "Y".

When we say ** if cmiss(of Bx{*}) ne 8 then new_X="Y";**

The maximum value is 8 since the length is 8

and if any of the 8 values is missing it wont equal 8 (it is less than 8) and we give "Y"

if cmiss equal to 8 it leaves a blank ....

cmiss value cant be greater than 8 in this case???am i right????

Also you have not used a Do Loop like Do i=1 to dim(i)..How can that be possible

Please suggest....

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

Posted in reply to robertrao

08-23-2016 01:30 AM

That's what of bx(*) does. It tells SAS to check all values in the array list.

There are other ways to specify variable lists,

Google 'SAS 9.4 Variable lists' and you'll find the documentation regarding variable lists.

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

Posted in reply to robertrao

08-23-2016 03:27 AM

You are correct. Since CMISS() count the missing value of a array, you can't get the number more than the number of members in this array.

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

Posted in reply to robertrao

08-23-2016 12:29 AM

So your logic is set new_x to Y if there's at least one value in the array?

Thats what your code currently accomplishes. If so, using CMISS or NMiSS is more efficient.

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

Posted in reply to Reeza

08-23-2016 12:31 AM

There's absolutely no reason you need to separate those steps. A datastep can have multiple array definitions and loops. Remove the data and set statements in between and your code is combined.

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

Posted in reply to Reeza

08-23-2016 12:55 AM

Thanks Reeza and Ksharp.

Yes, If there is atleast one non missing value after looping then i set the variable to "Y" and this holds good for both the arrays.

Learnt that Using CMISS is efficient way , since two of you have emphasized , but this below gives the same result as well...?????

My Question is that we are not using Do loop here, and how does SAS know that it has to loop????

Pardon my dumb question....

data array;

set have;

array Bx{8} Abx1-Abx8;

array BY{5} NAby1-NAby5;

if Bx{i} ne " " then new_X="Y";

if BY{i} ne " " then new_Y="Y";

end;

drop i;

run;

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

Posted in reply to robertrao

08-23-2016 01:01 AM

Um...not sure where you came up with that code. It would need a loop.

Verify the suggestions from KSharp and myself.

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

Posted in reply to robertrao

12-23-2016 12:25 PM

Hi,

I have data with transactions amounts every day with cust and trans ids. I want to calcuate sum of transactions and count of transactions for each month by custid using arrays. How do I do?

I have data with transactions amounts every day with cust and trans ids. I want to calcuate sum of transactions and count of transactions for each month by custid using arrays. How do I do?