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
if cmiss(of Bx{*}) ne 8 then new_X="Y"; if cmiss(of By{*}) ne 5 then new_Y="Y";
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;
Just to make sure , this code gives a "Y" if all the values are missing right????
Thanks
Wrong.
It matches your requested logic, if any variable in the list is not missing then it is set to Y.
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";
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.
if cmiss(of Bx{*}) ne 8 then new_X="Y"; if cmiss(of By{*}) ne 5 then new_Y="Y";
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....
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.
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.
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.
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;
Um...not sure where you came up with that code. It would need a loop.
Verify the suggestions from KSharp and myself.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.