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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

15 REPLIES 15
Ksharp
Super User
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;


 

robertrao
Quartz | Level 8

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

 

Thanks

Reeza
Super User

Wrong. 

 

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

robertrao
Quartz | Level 8

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

Reeza
Super User

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. 

Ksharp
Super User

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

robertrao
Quartz | Level 8

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

Reeza
Super User

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. 

 

Ksharp
Super User
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.
Reeza
Super User

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. 

Reeza
Super User

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. 

robertrao
Quartz | Level 8

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;

 

Reeza
Super User

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

 

Verify the suggestions from KSharp and myself. 

SM3
Calcite | Level 5 SM3
Calcite | Level 5
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?

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
  • 15 replies
  • 1924 views
  • 1 like
  • 4 in conversation