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

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 🙂

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Malthe
Obsidian | Level 7

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 🙂 

 

 

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
  • 2 replies
  • 3435 views
  • 1 like
  • 2 in conversation