data raw (rename= (visit= ELEMENT)); input pt visit :$13. STDT: is8601da.; format STDT is8601da.; datalines; 101 screening 2017-02-10 101 randomization 2017-02-24 101 treatment 2017-03-05 101 followup 2017-09-23 102 screening 2017-02-12 102 randomization 2017-02-22 102 treatment 2017-03-10 102 followup 2017-09-20 run;
Here seseq is taken according to alphebetical order of element
and TAESEQ is given according to order of occurence of element
Want:
pt element seseq TAESEQ101 screening 4 1 101 randomization 3 2 101 DrugA 1 3 101 followup 2 4 102 screening 4 1 102 randomization 3 2 102 DrugB 1 3 102 followup 2 4
Hope you checked the want2 dataset if you run my code which i shared earlier, the want2 dataset has the expected output.
please try the below code
data raw (rename= (visit= ELEMENT));
input pt visit :$13. STDT: is8601da.;
if visit='treatment' then visit='Drug A';
format STDT is8601da.;
datalines;
101 screening 2017-02-10
101 randomization 2017-02-24
101 treatment 2017-03-05
101 followup 2017-09-23
102 screening 2017-02-12
102 randomization 2017-02-22
102 treatment 2017-03-10
102 followup 2017-09-20
run;
proc sort data=raw;
by pt element;
run;
data want;
set raw;
by pt element;
retain seseq;
if first.pt then seseq=1;
else seseq+1;
run;
proc sort data=want;
by pt STDT;
run;
data want2;
set want;
by pt stdt;
retain taeseq;
if first.pt then taeseq=1;
else taeseq+1;
run;
even i followed the same procedure but wen combining the both i am unable to get the following output
pt element seseq TAESEQ101 screening 4 1 101 randomization 3 2 101 DrugA 1 3 101 followup 2 4 102 screening 4 1 102 randomization 3 2 102 DrugB 1 3 102 followup 2 4
Hope you checked the want2 dataset if you run my code which i shared earlier, the want2 dataset has the expected output.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.