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?
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;
What does your desired result look like?
So in this case, you want n new columns?
This kid of problems is much easier to solve with a long data set.
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;
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;
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.