SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Adding prefix to the visit based on previous or upcoming visits

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Adding prefix to the visit based on previous or upcoming visits

Hello Folks,

 

I have a typical situation here to handle.

 

Client gave me several visits and wanted to modify(adding prefix to unscheduled visits) the visits based on the previous or next visits, the problem here is we dont even know when the unscheduled visit comes.

 

Here is the snapshot of the scenario.

 

Kindly, help me with this

 

subject    visit        Expected Visit
101    baseline               baseline
101    unscheduled 01    MTN Unscheduled 01
101    unscheduled 02    MTN Unscheduled 02
101    unscheduled 03    MTN Unscheduled 03
101    MTN week 07        MTN week 07
101    MTN WEEK 13      MTN WEEK 13
101    REI baseline              REI baseline
101    REI WEEK 07             REI WEEK 07
101    UNSCHEDULED 01    REI UNSCHEDULED 01
101    UNSCHEDULED 02    REI UNSCHEDULED 02
103    REI WEEK 13            REI WEEK 13

 


Accepted Solutions
Solution
‎05-16-2018 05:38 AM
PROC Star
Posts: 1,288

Re: Adding prefix to the visit based on previous or upcoming visits

Well, I had to make a few assumptions about your desired logic, but here's a program that creates your desired output from your input.

 

Hope it helps!

   Tom

 

data Inter01;
set Have;
SeqNo = _n_;
run;

data Inter02;
length ExpectedVisit $20 OldPrefix $20 CurrentPrefix $20 FirstWord $20;
retain OldPrefix;
set Inter01;
by Subject;
if first.Subject then OldPrefix = " ";
FirstWord = scan(Visit, 1);
if lowcase(FirstWord) ^= "unscheduled" & lowcase(FirstWord) ^= "baseline"
then do;
OldPrefix = FirstWord;
ExpectedVisit = Visit;
end;
else if CurrentPrefix = "" & OldPrefix ^= ""
then do;
CurrentPrefix = OldPrefix;
if lowcase(FirstWord) = "unscheduled"
then ExpectedVisit = catx(" ", CurrentPrefix, Visit);
else ExpectedVisit = Visit;
end;
drop OldPrefix;
run;

proc sort data=Inter02 out=Inter03;
by descending SeqNo;
run;

data Inter04;
length ExpectedVisit $20 OldPrefix $20 CurrentPrefix $20 FirstWord $20;
retain OldPrefix;
set Inter03;
by notsorted Subject;
if first.Subject then OldPrefix = " ";
FirstWord = scan(Visit, 1);
if lowcase(FirstWord) ^= "unscheduled" & lowcase(FirstWord) ^= "baseline"
then do;
OldPrefix = FirstWord;
ExpectedVisit = Visit;
end;
else if CurrentPrefix = "" & OldPrefix ^= ""
then do;
CurrentPrefix = OldPrefix;
if lowcase(FirstWord) = "unscheduled"
then ExpectedVisit = catx(" ", CurrentPrefix, Visit);
else ExpectedVisit = Visit;
end;
run;

proc sql noprint;
create table Want as
select Subject, Visit, ExpectedVisit
from Inter04
order by SeqNo;
quit;

View solution in original post


All Replies
Occasional Contributor
Posts: 19

Re: Adding prefix to the visit based on previous or upcoming visits

sorry typo, last visit is also of the same subject 101

Solution
‎05-16-2018 05:38 AM
PROC Star
Posts: 1,288

Re: Adding prefix to the visit based on previous or upcoming visits

Well, I had to make a few assumptions about your desired logic, but here's a program that creates your desired output from your input.

 

Hope it helps!

   Tom

 

data Inter01;
set Have;
SeqNo = _n_;
run;

data Inter02;
length ExpectedVisit $20 OldPrefix $20 CurrentPrefix $20 FirstWord $20;
retain OldPrefix;
set Inter01;
by Subject;
if first.Subject then OldPrefix = " ";
FirstWord = scan(Visit, 1);
if lowcase(FirstWord) ^= "unscheduled" & lowcase(FirstWord) ^= "baseline"
then do;
OldPrefix = FirstWord;
ExpectedVisit = Visit;
end;
else if CurrentPrefix = "" & OldPrefix ^= ""
then do;
CurrentPrefix = OldPrefix;
if lowcase(FirstWord) = "unscheduled"
then ExpectedVisit = catx(" ", CurrentPrefix, Visit);
else ExpectedVisit = Visit;
end;
drop OldPrefix;
run;

proc sort data=Inter02 out=Inter03;
by descending SeqNo;
run;

data Inter04;
length ExpectedVisit $20 OldPrefix $20 CurrentPrefix $20 FirstWord $20;
retain OldPrefix;
set Inter03;
by notsorted Subject;
if first.Subject then OldPrefix = " ";
FirstWord = scan(Visit, 1);
if lowcase(FirstWord) ^= "unscheduled" & lowcase(FirstWord) ^= "baseline"
then do;
OldPrefix = FirstWord;
ExpectedVisit = Visit;
end;
else if CurrentPrefix = "" & OldPrefix ^= ""
then do;
CurrentPrefix = OldPrefix;
if lowcase(FirstWord) = "unscheduled"
then ExpectedVisit = catx(" ", CurrentPrefix, Visit);
else ExpectedVisit = Visit;
end;
run;

proc sql noprint;
create table Want as
select Subject, Visit, ExpectedVisit
from Inter04
order by SeqNo;
quit;
Occasional Contributor
Posts: 19

Re: Adding prefix to the visit based on previous or upcoming visits

Thank you so much Tom its perfectly working...!!!!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 174 views
  • 0 likes
  • 2 in conversation