BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Anita_n
Pyrite | Level 9

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

What does your desired result look like?

Anita_n
Pyrite | Level 9
something like this

dos2_cum=dos1 + dos2;
dos3_cum=dos2_cum+ dos3;
dos4_cum=dos3_cum + dos4;
.
.
.
dosn_cum=dos3_cum + dos_n;

PeterClemmensen
Tourmaline | Level 20

So in this case, you want n new columns?

 

This kid of problems is much easier to solve with a long data set.

Anita_n
Pyrite | Level 9
I wish to use a do while statement or any other loop type to do that, since I don't know how many nth terms of dos in the dataset is.
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;
Kurt_Bremser
Super User

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;
Ksharp
Super User
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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 788 views
  • 2 likes
  • 4 in conversation