Hi there and in advance thanks a lot for your help! I'm using SAS Enterprise Guide 7.15.
I have a dataset with patients, baseline-dates and scoring-dates. For each patient i have the same baseline-date and multiple scoring-dates (>20). I want to see whether the scoring-dates are within certain periods of time from the baseline-date: 1 year, 2 years, 3 years ... - 100 years;
Instead of writing multiple lines of codes, i am trying to boil it down to a more manageable size.
Here is a data-example with five scoring dates:
data one; length patient_id $ 8; format baseline_date score_date date9.; input patient_id baseline_date score_date; cards; Patient1 0 200 Patient1 0 400 Patient1 0 600 Patient1 0 800 Patient1 0 1000 ; run;
Here is my code:
data scoring_within_year_x; set one; array a_withinyear [5] (1 2 3 5 10 100); /* For simplicity boiled down to smaller fixed array, these are the values i need the most */ do i = 1 to dim(a_withinyear); if intnx('year',baseline_date,a_withinyear[i],'same' >= score_date then /* Here i have my first problem, i want to name the new variable as a concatenation of a string and the value of the array - something like this */ cats('Score_within_',a_withinyear[i],'_year') = 1
/* Here i have my second problem. I want to output multiple variables to the same observation as shown underneath */ end; run;
So ideally my output for the 4th line of the original data would look like this:
Patient_id | Baseline_date | Score_date | Score_within_1_year | Score_within_2_year | Score_within_5_year | etc...
Patient1 0 800 1 1 0
I hope you can help me 🙂
Hi,
try Proc Transpose:
data one;
length patient_id $ 8;
format baseline_date score_date date9.;
input patient_id baseline_date score_date;
cards;
Patient1 0 200
Patient1 0 400
Patient1 0 600
Patient1 0 800
Patient1 0 1000
;
run;
data tmp;
set one;
array a_withinyear [6] _temporary_ (1 2 3 5 10 100) ;
/* For simplicity boiled down to smaller fixed array, these are the values i need the most */
do i = 1 to dim(a_withinyear);
if intnx('year',baseline_date,a_withinyear[i],'same') >= score_date then
do;
varname = cats('Score_within_',a_withinyear[i],'_year');
flag = 1;
output;
end;
end;
run;
proc sort data=tmp;
by patient_id baseline_date score_date varname;
run;
proc transpose data = tmp out = scoring_within_year_x(drop = _name_);
by patient_id baseline_date score_date;
var flag;
id varname;
run;
Bart
Hi,
try Proc Transpose:
data one;
length patient_id $ 8;
format baseline_date score_date date9.;
input patient_id baseline_date score_date;
cards;
Patient1 0 200
Patient1 0 400
Patient1 0 600
Patient1 0 800
Patient1 0 1000
;
run;
data tmp;
set one;
array a_withinyear [6] _temporary_ (1 2 3 5 10 100) ;
/* For simplicity boiled down to smaller fixed array, these are the values i need the most */
do i = 1 to dim(a_withinyear);
if intnx('year',baseline_date,a_withinyear[i],'same') >= score_date then
do;
varname = cats('Score_within_',a_withinyear[i],'_year');
flag = 1;
output;
end;
end;
run;
proc sort data=tmp;
by patient_id baseline_date score_date varname;
run;
proc transpose data = tmp out = scoring_within_year_x(drop = _name_);
by patient_id baseline_date score_date;
var flag;
id varname;
run;
Bart
Great solution, and quick as well! Thanks!
I actually solved it myself before seeing your answer using a 'call execute - datastep-macro' instead, but i think i like your solution better. It must be quicker than to read and write to the same data-set many times 🙂
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!
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.