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
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
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
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;
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 😉
And how can i name them in array called A and B? thanks
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?
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
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;
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
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;
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
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
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.