BookmarkSubscribeRSS Feed
abcdefg
Calcite | Level 5

Hi everyone.

If I got a data like:

1 1 4 6 7 3 2 5 7 8 9 5 3 2 6 8 9 0 9 0 9 7 6 5 4 5 7 8 9 8

how can i put them into different arrays alternatively? like the first, third, fifth, seventh.....,29th (the number of obeservation = old) in one group and the second, forth, sixth,....,30th (the number of obersvation = even) in another group.

Thank you

12 REPLIES 12
Tom
Super User Tom
Super User

What do you really want to do?  I don't think that ARRAY (as used in SAS) has anything to do with your problem.

If you want to read in a series of numbers into two variables (call them ODD and EVEN) then you can do it this way.

data want;

input odd even @@;

cards;

1 1 4 6 7 3 2 5 7 8 9 5 3 2 6 8 9 0 9 0 9 7 6 5 4 5 7 8 9 8

;;;;

proc print; run;

Obs    odd    even

  1     1       1

  2     4       6

  3     7       3

  4     2       5

  5     7       8

  6     9       5

  7     3       2

  8     6       8

  9     9       0

10     9       0

11     9       7

12     6       5

13     4       5

14     7       8

15     9       8

abcdefg
Calcite | Level 5

My question mainly is I would like to group the 1st, 3rd, 5th, 7th, 9th,....,29th obervation in ONE group called "A"

then another group called "B" for the 2nd, 4th, 6th,..., 30th obeservation.

I would like to use the array function to do this.

Thanks

art297
Opal | Level 21

Tom is probably right but, if you already have the file and need to split the variables into two groups, you could use something like:

data have;

  input x1-x30;

  cards;

1 1 4 6 7 3 2 5 7 8 9 5 3 2 6 8 9 0 9 0 9 7 6 5 4 5 7 8 9 8

;

data want;

  set have;

  array all(*) x1-x30;

  array odd(15);

  array even(15);

  do i=1 to dim(all)-1 by 2;

    j+1;

    odd(j)=all(i);

    even(j)=all(i+1);

  end;

run;

abcdefg
Calcite | Level 5

But i actually got 1044 data, is there a way that I can avoid typing that 1044 data for this command as in the 7th line of your reply? (the line under cards 😉

abcdefg
Calcite | Level 5

And how can i name them in array called A and B? thanks

art297
Opal | Level 21

I'm unclear about your data.  Do you have something like 1044 records with one variable each, or one (or more) record(s) with each containing 1044 variables?

In either case, what are the actual variable names?

abcdefg
Calcite | Level 5

I do have 1044 data, which the data are alternatively placed (1st data - group A, 2nd data - group B, 3rd data - group A, .... , 1044th data - group B)

thank you

art297
Opal | Level 21

I'm still unclear about what you have and what you want to have.  Does the following describe your data and a possible solution?

data have;

  input data;

  cards;

1

1

4

6

7

3

2

5

7

8

9

5

3

2

6

8

9

0

9

0

9

7

6

5

4

5

7

8

9

8

;

data want;

  set have;

  retain groupA;

  if mod(_n_,2) then groupA=data;

  else do;

    groupB=data;

    output;

  end;

run;

Haikuo
Onyx | Level 15

When you mention that you have 1044 data, do you mean you have 1044 columns (variables) or 1044 rows (records)? Either way, I think you already have great answers, unless your '1044 data' means otherwise, which you need to let us know, perferably with some sample data.

Haikuo

Linlin
Lapis Lazuli | Level 10

Is this what you want?

data have;

  input v1;

  cards;

1

1

4

6

7

3

2

5

7

8

9

5

3

2

6

8

9

0

9

0

9

7

6

5

4

5

7

8

9

8

;

data temp;

  length group $ 1;

  set have;

  n=_n_;

  if mod(_n_,2) then group='A';

    else group='B';

run;

proc sort;by group n;run;

proc transpose data=temp out=want ;

var v1;

run;

data want;

   set want;

   array A(*) col1-col15;

   array B(*) col16-col30;

   do i=1 to 15;

     total_A +A(i);

     total_B +B(i);

     end;

run;

proc print;run;

Tom
Super User Tom
Super User

If you want to create assign alternate observations to groups .

data want ;

   retain group ;

  set have;

  if group='A' then group='B';

   else group='A';

run;

Obs    group    odd    even

  1      A       1       1

  2      B       4       6

  3      A       7       3

  4      B       2       5

  5      A       7       8

  6      B       9       5

  7      A       3       2

  8      B       6       8

  9      A       9       0

10      B       9       0

11      A       9       7

12      B       6       5

13      A       4       5

14      B       7       8

15      A       9       8

Ksharp
Super User

It looks like OP did not make himself clear.

data have;
input value @@;
cards;
1 1 4 6 7 3 2 5 7 8 9 5 3 2 6 8 9 0 9 0 9 7 6 5 4 5 7 8 9 8
;
run;
%let dsid=%sysfunc(open(have));
%let n=%sysevalf(%sysfunc(attrn(&dsid,nobs))/2,ceil);
%let dsid=%sysfunc(close(&dsid));


data _null_;
 set have end=last;
 array odd{&n} _temporary_;
 array even{&n} _temporary_;
 if mod(_n_,2) eq 1 then do; a+1;odd{a}=value;end;
  else do; b+1;even{b}=value;end;

if last then do;
do i=1 to &n;
 put odd{i}= even{i}=;
end;
end;
run;

Ksharp

Tian.Kong

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 12 replies
  • 1148 views
  • 0 likes
  • 6 in conversation