DATA Step, Macro, Functions and more

Arrays

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Arrays

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
Super User
Posts: 9,671

Re: Arrays


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

View solution in original post


All Replies
Super User
Posts: 9,671

Re: Arrays

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;


 

Super Contributor
Posts: 1,040

Re: Arrays

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

 

Thanks

Super User
Posts: 17,775

Re: Arrays

Wrong. 

 

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

Super Contributor
Posts: 1,040

Re: Arrays

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

Super User
Posts: 17,775

Re: Arrays

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
Super User
Posts: 9,671

Re: Arrays


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

Super Contributor
Posts: 1,040

Re: Arrays

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

Super User
Posts: 17,775

Re: Arrays

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. 

 

Super User
Posts: 9,671

Re: Arrays

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.
Super User
Posts: 17,775

Re: Arrays

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. 

Super User
Posts: 17,775

Re: Arrays

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. 

Super Contributor
Posts: 1,040

Re: Arrays

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;

 

Super User
Posts: 17,775

Re: Arrays

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

 

Verify the suggestions from KSharp and myself. 

Occasional Contributor SM3
Occasional Contributor
Posts: 9

Re: Arrays

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?
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 530 views
  • 1 like
  • 4 in conversation