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

Dear Experts

 

I have the following data in the three first columns and would like to assign the number in column four (meeting number). The number is based on the date. If this shifts then a new number has to be assigned. The data set includes numerus other variables.

 

Record_idAdmission numberDateMeeting number
1101-01-20011
1101-01-20011
1101-03-20012
1201-05-20011
1201-05-20011
1201-05-20011
    

 

Kind regards

 

Solvej

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
infile cards expandtabs;
input Record_id	Admission 	Date : $20.;
cards;
1	1	01-01-2001	1
1	1	01-01-2001	1
1	1	01-03-2001	2
1	2	01-05-2001	1
1	2	01-05-2001	1
1	2	01-05-2001
;
run;


data want;
set have;
by record_id admission date;
k=lag(Date);
if first.Admission then meeting_number=1;
else if k ne date then meeting_number+1;
drop k;
run;

View solution in original post

5 REPLIES 5
Shmuel
Garnet | Level 18

Assuming data is sorted by the 3 coloms then do:

data want;
  set have;
       by record_id Admission_nomber date;
       retain meeting_number;

      if first.date then meeting_number = 1;
      else meeting_number +1;
run;
Astounding
PROC Star

I think that's in the ballpark, but you need to tweak the logic a bit:

 

data want;
  set have;
       by record_id Admission_number date;
      if first.admission_number then meeting_number = 1;
      else if first.date then meeting_number +1;
run;

shantanupl1
Obsidian | Level 7

Hi,

 

Try this code.

 

data test;
set abc;
by record_id adm_no;
retain hold .;
if first.adm_no then do;
hold=date;
meeting=1;
end;
if hold ne date then do;
hold = date;
meeting + 1;
end;
run;

Ksharp
Super User
data have;
infile cards expandtabs;
input Record_id	Admission 	Date : $20.;
cards;
1	1	01-01-2001	1
1	1	01-01-2001	1
1	1	01-03-2001	2
1	2	01-05-2001	1
1	2	01-05-2001	1
1	2	01-05-2001
;
run;
data want;
 set have;
 by Record_id	Admission	Date;
 if first.Admission then want=0;
 want+first.date;
run;
novinosrin
Tourmaline | Level 20
data have;
infile cards expandtabs;
input Record_id	Admission 	Date : $20.;
cards;
1	1	01-01-2001	1
1	1	01-01-2001	1
1	1	01-03-2001	2
1	2	01-05-2001	1
1	2	01-05-2001	1
1	2	01-05-2001
;
run;


data want;
set have;
by record_id admission date;
k=lag(Date);
if first.Admission then meeting_number=1;
else if k ne date then meeting_number+1;
drop k;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 863 views
  • 0 likes
  • 6 in conversation