Help using Base SAS procedures

Matrix Table query

Occasional Contributor
Posts: 9

Matrix Table query


I hope this the right place for this query, so apologies if not.

I'm going to receive a table as a csv file, and its going to be in this format:

              a          a          b          b

              a1        a2       b1         b2   

a     a1     x          1          1          1

a     a2     1          x          1          1

b     b1     1          1          x          1

b     b2     1          1          1          x

where a & b are a variable, and a1/b1 etc are subsets of that variable.

Is it possible to aggregate the table so becomes:

               a          b

a             x          2

b             2          x

a,a or b,b are always going to be x or zero, so some data (in this case 1) are going to be ignored

Many Thanks in advance,

Super Contributor
Posts: 490

Re: Matrix Table query

Posted in reply to AntonyBlunt

what is the formula or the function you used to aggregate the matrix

Occasional Contributor
Posts: 9

Re: Matrix Table query

Posted in reply to mohamed_zaki


In the above tables?  I just typed those in as a small example of what I need to do. 

Some pointers in formula / function to aggregate the data are what I am after (reading my post, I think I might have been a bit ambiguous).

Many Thanks

Super Contributor
Posts: 319

Re: Matrix Table query

Posted in reply to AntonyBlunt


If I have correctly understood - here is one solution:

data have;
infile datalines;
input  (vars  subvars a1 a2 b1 b2 c1 c2 d1 d2) ($);
a a1 x  1  1  1 5 6 1 1
a a2 1  x  1  1 5 6 1 1
b b1 1  1  x  1 2 3 1 1
b b2 1  1  1  x 2 3 1 1
c c1 2  3  4  5 x 1 1 1
c c2 2  3  4  5 1 x 1 1
d d1 1  1  1  1 1 1 x 1
d d2 1  1  1  1 1 1 1 x

proc sql noprint;
select distinct vars into :vars separated by ' ' from have;

data want;
set have;
array newvars{*} $ &vars;
array oldvars{*} $ a1--d2;

do i=1 to dim(newvars);
if vars=vname(newvars{i}) then newvars{i}='x';
else newvars{i}=left(put(input(oldvars{2*(i-1)+1},8.)+input(oldvars{2*(i-1)+2},8.),8.));

keep vars &vars;


/*remove duplicates*/
proc sort data=want out=want noduprecs;
by _all_;

Occasional Contributor
Posts: 9

Re: Matrix Table query

Thankyou very much for that, it looks very good indeed. Smiley Happy

Just one more question (sorry - I am verrry new to SAS, and have certainly be thrown in at the deepend with this...)

the table i will be working with is going to contain many many more variables, and is going to be imported as a csv.

In your example I ammended the infile line to point to a csv file which had the same variable names as the input line, and removed the "datalines"

Is it possible to automatically import all the variables without having to list each one in the input line?

Ask a Question
Discussion stats
  • 4 replies
  • 3 in conversation