BookmarkSubscribeRSS Feed
AntonyBlunt
Calcite | Level 5

Hi,

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,

4 REPLIES 4
mohamed_zaki
Barite | Level 11

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

AntonyBlunt
Calcite | Level 5

Hi,

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

Loko
Barite | Level 11

Hello,

If I have correctly understood - here is one solution:

data have;
infile datalines;
input  (vars  subvars a1 a2 b1 b2 c1 c2 d1 d2) ($);
datalines;
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;
quit;


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.));
end;

keep vars &vars;

run;

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

AntonyBlunt
Calcite | Level 5

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?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1091 views
  • 3 likes
  • 3 in conversation