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

hello. i would like to ask about cumulative numbering of clinic type.

 

i've been asked about various type of cumulative calculation in this community. i'm so thankful for all your help so far.

 

however, 'hash' command is very difficult to apply in new question. so, i asked one more question.

 

i want to calculative type of clinic cumulatively. raw data is here....

 

data have;
input id date clinic ;
cards;
1 20020408 12135
1 20020715 12135
1 20030124 15109
1 20030308 75423
1 20030505 75423
1 20030607 75423
1 20030815 12135
2 20020615 25875
2 20020717 25875
2 20020824 55415
2 20020907 25875
2 20021215 25875
2 20030108 25875

 

and i want to make this raw data like this....

 

data want;
input id date clinic type;
cards;
1 20020408 12135 1
1 20020715 12135 1
1 20030124 15109 2
1 20030308 75423 3
1 20030505 75423 3
1 20030607 75423 3
1 20030815 12135 3
2 20020615 25875 1
2 20020717 25875 1
2 20020824 55415 2
2 20020907 25875 2
2 20021215 25875 2
2 20030108 25875 2

 

as you can see in 7th row, the numbering of type is still same, because clinic '12135' is not new type of clinic for 'id 1'

but in 10th row, 1 was added to the numbering of type, because clinic '55415' is new type of clinic for 'id 2'

 

i tried this code, but the results didn't worked out.

 

 

data want;
set have;
by id;
if _n_=1 the do;
declare hash h ();
h.definekey('clinic');
h.definedata('type');
h.definedone();
end;

 

if first.id then h.clear();
if h.find()=0 then do;
type=1;
h.replace();
end;

if h.find()^=0 then do;
type=type+1;
h.replace();
run;

 

i really appreciate for all your help.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Like this?

 


data WANT;
  length LIST $32000;
  retain LIST ;
  drop LIST;
  set HAVE; 
  by ID;
  if first.ID then do;
    LIST=cat(CLINIC);
    TYPE=1;
  end;
  else if ^findw(LIST,catt(CLINIC)) then do;
    TYPE+1;
    LIST=catx(' ',LIST,CLINIC);
  end;
run;

 

Hash tables are not the universal answer.

 

id date clinic TYPE
1 20020408 12135 1
1 20020715 12135 1
1 20030124 15109 2
1 20030308 75423 3
1 20030505 75423 3
1 20030607 75423 3
1 20030815 12135 3
2 20020615 25875 1
2 20020717 25875 1
2 20020824 55415 2
2 20020907 25875 2
2 20021215 25875 2
2 20030108 25875 2

View solution in original post

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

Like this?

 


data WANT;
  length LIST $32000;
  retain LIST ;
  drop LIST;
  set HAVE; 
  by ID;
  if first.ID then do;
    LIST=cat(CLINIC);
    TYPE=1;
  end;
  else if ^findw(LIST,catt(CLINIC)) then do;
    TYPE+1;
    LIST=catx(' ',LIST,CLINIC);
  end;
run;

 

Hash tables are not the universal answer.

 

id date clinic TYPE
1 20020408 12135 1
1 20020715 12135 1
1 20030124 15109 2
1 20030308 75423 3
1 20030505 75423 3
1 20030607 75423 3
1 20030815 12135 3
2 20020615 25875 1
2 20020717 25875 1
2 20020824 55415 2
2 20020907 25875 2
2 20021215 25875 2
2 20030108 25875 2
novinosrin
Tourmaline | Level 20

data have;
input id date :yymmdd10. clinic ;
format date yymmdd10.;
cards;
1 20020408 12135
1 20020715 12135
1 20030124 15109
1 20030308 75423
1 20030505 75423
1 20030607 75423
1 20030815 12135
2 20020615 25875
2 20020717 25875
2 20020824 55415
2 20020907 25875
2 20021215 25875
2 20030108 25875
;

data want;
if 0 then set have;
array t(999) _temporary_;
call missing(of t(*),type);
do until(last.id);
set have;
by id ;
if clinic not in t then do;
type+1;
t(type)=clinic;
end;
output;
end;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 643 views
  • 4 likes
  • 3 in conversation