data even_odd;
array v[20] value1-value20;
do values= 1 to 20;
v[values]=values;
end;
drop values;
run;
data even_values;
set even_odd;
array v[20] value1-value20;
do values = 1 to 20;
if mod(values,2)=0 then output;
end;
drop values;
run;
How to get even and odd values using above array
Is this what you want?
data even_values;
set even_odd;
array v[20] value1-value20;
do values = 1 to 20;
if mod(v(values),2)=0 then output;
end;
drop value1-value20;
run;
Of course, this only works because you have set up the data such that (for example) v(18) is equal to 18, and v(17) is equal to 17, and so on. If you want code for a more general case, where the value of v(18) could be 3, then the above code will not work. So, do you want to handle this more general case? Please state clearly.
Run PROC TRANSPOSE on the results of my code.
It's very unclear what you're asking for. Hopefully below code will give you at least some guidance for what you are really trying to achieve.
data even;
array value_[20] 8;
do i= 1 to dim(value_);
value_[i]=i*2;
end;
drop i;
run;
proc print data=even;
run;
I don't understand what an EVEN value of an ARRAY means.
You can test if particular variables in the array have odd or even values, but a test for the full array makes no sense.
Do you want convert your array back into a series of observations?
Perhaps at that point keep only the even (or odd) values? Or split them into two different datasets?
data evens odds;
set even_odd;
array v value1-value20;
do index=1 to dim(v);
value=v[index];
if 0=mod(value,2) then output evens;
else output odds;
end;
run;
Or perhaps keep only the values of the odd (or even) variable numbers?
data evens odds;
set even_odd;
array v value1-value20;
do index=1 to dim(v);
value=v[index];
if 0=mod(index,2) then output evens;
else output odds;
end;
run;
Do you want to convert the even (or odd) values in the array to missing?
data odds;
set even_odd;
array v value1-value20;
do index=1 to dim(v);
if 0=mod(v[index],2) then v[index]=.;
end;
run;
Hi Tom,
I want horizantal rows even and odd with mod function required output like below
value2 | value4 | value6 | value8 | value10 | ||||
2 | 4 | 6 | 8 | 10 |
So you want some kind of report with some completely empty columns (without header)?
Here's a third vote for "I haven't a clue what you actually want".
Show us.
You don't even mention why you don't like the result of your current code. I suspect something related to having the exact same output 10 times with all the variables and all the values the same.
@pavank wrote:
data even_odd; array v[20] value1-value20; do values= 1 to 20; v[values]=values; end; drop values; run; data even_values; set even_odd; array v[20] value1-value20; do values = 1 to 20; if mod(values,2)=0 then output; end; drop values; run;
How to get even and odd values using above array
Do you need something like this:
[EDIT: small I/O update]
data even_odd;
array v[20] value1-value20 (1:20);
run;
data _null_;
set even_odd(obs=1);
array v value1-value20;
length odd_list even_list $ 32767;
do over V;
if mod(_I_,2)=1
then odd_list = catx(" ", odd_list, vname(v));
else even_list = catx(" ", even_list, vname(v));
end;
call execute('data even(keep=' !! strip(even_list) !! ') odd(keep=' !! strip(odd_list) !! '); set even_odd; run;');
stop;
run;
Bart
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.