BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Hi,

I need to list out the medical stages of the patients. In the output the values of each stage should be unique in order. I shared the example set below.

data have;
input ID Date1 mmddyy10. stages$10.;
format Date1 mmddyy10.;
cards;
123 02/05/2000 7,2,2
456 08/08/1999 5,5,1
789 07/02/2012 1,1
187 06/06/2003 7,9
753 12/12/1993 2,9,2,2,2
654 01/11/2001 2,9,2
321 04/05/2000 12,5,9,5
987 03/02/1999 14
178 05/06/1998 5
845 03/03/2020 9,9,9
159 09/08/2020 1,3,18
run;

Expected output : 

stages Stg
7,2,2 7,2
5,5,1 5,1
1,1 1
7,9 7,9
2,9,2,2,2 9,2
2,9,2 9,2
12,5,9,5 12,9,5
14 14
5 5
9,9,9 9
1,3,18 1,3,18

The values should matches the corresponding ID and date.

Kindly suggest a code.

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @Sathish_jammy  There are multiple ways to approach the solution to your question. The idea is to evaluate

1. what would be the shortest path or the quickest path?

2. Whether linear or random binary search

 

A slow linear could mean many approaches which will get the job done perhaps using REGEX and other methods and so forth.  I personally would like to approach with a HASH solution as I am biased to approaches/methodology devised and taught by Guru Paul D

 



data have;
input ID Date1 mmddyy10. stages$10.;
format Date1 mmddyy10.;
cards;
123 02/05/2000 7,2,2
456 08/08/1999 5,5,1
789 07/02/2012 1,1
187 06/06/2003 7,9
753 12/12/1993 2,9,2,2,2
654 01/11/2001 2,9,2
321 04/05/2000 12,5,9,5
987 03/02/1999 14
178 05/06/1998 5
845 03/03/2020 9,9,9
159 09/08/2020 1,3,18
;


data want;
 if _n_=1 then do;
  dcl hash H () ;
  h.definekey  ("_temp") ;
  h.definedone () ;
 end;
 set have;
 length stg $100;
 do _n_=1 to countw(stages,',');
  _temp=scan(stages,_n_,',');
  if h.check() ne 0 then do;
   Stg=catx(',',Stg,_temp);
   h.add();
  end;
 end;
 h.clear();
 keep stages stg;
run;

 

View solution in original post

1 REPLY 1
novinosrin
Tourmaline | Level 20

HI @Sathish_jammy  There are multiple ways to approach the solution to your question. The idea is to evaluate

1. what would be the shortest path or the quickest path?

2. Whether linear or random binary search

 

A slow linear could mean many approaches which will get the job done perhaps using REGEX and other methods and so forth.  I personally would like to approach with a HASH solution as I am biased to approaches/methodology devised and taught by Guru Paul D

 



data have;
input ID Date1 mmddyy10. stages$10.;
format Date1 mmddyy10.;
cards;
123 02/05/2000 7,2,2
456 08/08/1999 5,5,1
789 07/02/2012 1,1
187 06/06/2003 7,9
753 12/12/1993 2,9,2,2,2
654 01/11/2001 2,9,2
321 04/05/2000 12,5,9,5
987 03/02/1999 14
178 05/06/1998 5
845 03/03/2020 9,9,9
159 09/08/2020 1,3,18
;


data want;
 if _n_=1 then do;
  dcl hash H () ;
  h.definekey  ("_temp") ;
  h.definedone () ;
 end;
 set have;
 length stg $100;
 do _n_=1 to countw(stages,',');
  _temp=scan(stages,_n_,',');
  if h.check() ne 0 then do;
   Stg=catx(',',Stg,_temp);
   h.add();
  end;
 end;
 h.clear();
 keep stages stg;
run;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1 reply
  • 580 views
  • 0 likes
  • 2 in conversation