Dear,
I need to create an additional record with sum values of two records. In the below output i need to create the red highlighted record.
data one;
input val$ n1 n2 total;
datalines;
cr 10 12 22
pr 12 23 35
sd 23 24 47
ne 30 35 65
;
Please suggest.
output needed:
val n1 n2 total
cr 10 12 22
pr 12 23 35
cr/pr 22 35 57
sd 23 24 47
ne 30 35 65
What is the rule that identifies the observations that need to be added? Is it only the position in the dataset?
This isn't particularly elegant, but it will do the job if what you've posted is literally all you want.
data one;
DROP _:;
RETAIN _CR 0;
RETAIN _CR_n1;
RETAIN _CR_n2;
RETAIN _CR_Total;
INPUT val$ n1 n2 total;
if Val = 'cr' THEN
DO;
_CR = 1;
_CR_n1 = n1;
_CR_n2 = n2;
_CR_Total = Total;
END;
if Val = 'pr' THEN
_PR = 1;
IF _CR AND _PR THEN
DO;
OUTPUT;
Val = 'cr/pr';
n1 = n1 + _CR_n1;
n2 = n2 + _CR_n2;
Total = Total + _CR_Total;
OUTPUT;
END;
ELSE
DO;
OUTPUT;
END;
datalines;
cr 10 12 22
pr 12 23 35
sd 23 24 47
ne 30 35 65
;
RUN;
Results:
If that's really all you need, please mark the solution; otherwise, please post more including more extensive data.
Jim
data want:
set have;
_n1 + n1;
_n2 + n2;
output;
if _n_ = 2
then do;
val = "cr/pr";
n1 = _n1;
n2 = _n2;
total = n1 + n2;
output;
end;
drop _:;
run;
Untested, posted from my tablet.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.