Help using Base SAS procedures

Array that perform this

Accepted Solution Solved
Reply
Super Contributor
Posts: 303
Accepted Solution

Array that perform this

[ Edited ]

Hello I have this data that I wish to createABC from Data AB  as follows:

 

Data A;

imput A B;

cards;

1   3

0   6

0   7

1  9

1  4

0  2

;

run;

Data ABC;

input A B C;

cards;

1   3   C=3+6+7+9+4+2=31

0  6   C=6

1  9   C=9+4+2=15

1  4   C=4+6

0   2  C=2

;

 

Thanks

 


Accepted Solutions
Solution
‎10-17-2015 10:46 PM
Respected Advisor
Posts: 4,920

Re: Array that perform this

Posted in reply to desireatem

Can be done with a double do loop:

 


data ABC;
do until(done);
    set a end=done;
    totSum + B;
    end;
done = 0;
do until(done);
    set a end=done;
    if A then C = totSum; else C = B;
    output;
    totSum + (-B);
    end;
keep A B C;
run;

proc print data=ABC noobs; run;
PG

View solution in original post


All Replies
Solution
‎10-17-2015 10:46 PM
Respected Advisor
Posts: 4,920

Re: Array that perform this

Posted in reply to desireatem

Can be done with a double do loop:

 


data ABC;
do until(done);
    set a end=done;
    totSum + B;
    end;
done = 0;
do until(done);
    set a end=done;
    if A then C = totSum; else C = B;
    output;
    totSum + (-B);
    end;
keep A B C;
run;

proc print data=ABC noobs; run;
PG
Valued Guide
Posts: 765

Re: Array that perform this

[ Edited ]
Posted in reply to desireatem

Hi, no array but another way  ...

 

data a;
input a b @@;
datalines;
1 3 0 6 0 7 1 9 1 4 0 2
;

 

* place sum of values of variable B in macro variable &TOT;

proc sql noprint;
select sum(b) into :tot from a;
quit;

 

data want (keep=a b c);
set a;
cumb + lag(b);

c = a*(&tot-cumb) + ^a*b;
run;

 

data set WANT ...

Obs    a    b     c
 1     1    3    31
 2     0    6     6
 3     0    7     7
 4     1    9    15
 5     1    4     6
 6     0    2     2

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 286 views
  • 0 likes
  • 3 in conversation