- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Dear all,
I have a dataset with like this
pid medi dos1 dos2 dos3 ......dos_n sex
1 A 5 5 2 male
2 B 3 1 3 female
3 C 4 9 4 male
4 D 2 8 5 female
please note that the number of variable dosis could be up to 10 or even more.
I wish to create a new dataset with the cummulative frequencies of dosis1 through dosis_n for each patient/sex using either a datastep or proc sql. any help?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input pid medi $ dos1 dos2 dos3 ;
cards;
1 A 5 5 2 male
2 B 3 1 3 female
3 C 4 9 4 male
4 D 2 8 5
;
data want;
set have;
array x{*} dos1-dos3;
array y{*} cum_dos1-cum_dos3;
cum=0;
do i=1 to dim(x);
cum+x{i};y{i}=cum;
end;
drop i cum;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What does your desired result look like?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
dos2_cum=dos1 + dos2;
dos3_cum=dos2_cum+ dos3;
dos4_cum=dos3_cum + dos4;
.
.
.
dosn_cum=dos3_cum + dos_n;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So in this case, you want n new columns?
This kid of problems is much easier to solve with a long data set.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
like
do while n ^=dos_n
dos2_cum=dos1 + dos2;
dos3_cum=dos2_cum+ dos3;
dos4_cum=dos3_cum + dos4;
.
.
.
dosn_cum=dos3_cum + dos_n;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Such sequences are much better stored in a long dataset, which makes calculations of running totals very simple:
data have;
input pid $ medi $ dos1 dos2 dos3 sex $;
datalines;
1 A 5 5 2 male
2 B 3 1 3 female
3 C 4 9 4 male
4 D 2 8 5 female
;
proc transpose
data=have
out=long (rename=(col1=dose))
;
by pid;
var dos:;
run;
data want;
merge
have (keep=pid medi sex)
long
;
by pid;
seq = input(compress(_name_,"","kd"),best.);
if first.pid
then cum_dose = dose;
else cum_dose + dose;
drop _name_;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input pid medi $ dos1 dos2 dos3 ;
cards;
1 A 5 5 2 male
2 B 3 1 3 female
3 C 4 9 4 male
4 D 2 8 5
;
data want;
set have;
array x{*} dos1-dos3;
array y{*} cum_dos1-cum_dos3;
cum=0;
do i=1 to dim(x);
cum+x{i};y{i}=cum;
end;
drop i cum;
run;