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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 781 views
  • 3 likes
  • 3 in conversation