BookmarkSubscribeRSS Feed
pavank
Quartz | Level 8
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 

10 REPLIES 10
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
pavank
Quartz | Level 8
Hi Paige,
I want Horizontal way val1-val20
PaigeMiller
Diamond | Level 26

Run PROC TRANSPOSE on the results of my code.

--
Paige Miller
Patrick
Opal | Level 21

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;

Patrick_0-1699756223388.png

 

Tom
Super User Tom
Super User

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;
pavank
Quartz | Level 8

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
PaigeMiller
Diamond | Level 26

@pavank 

 

It seems that neither @Tom nor I really know what you want as output. Don't make us guess. Show us a screen capture of the desired output; use the "Insert Photos" icon to include your screen capture in your reply; do NOT attach files.

--
Paige Miller
ballardw
Super User

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 


 

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1899 views
  • 0 likes
  • 7 in conversation