BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

I have a raw dataset with 200 observations.There is a date and time variable which records date and time when the patient information was entered into the database.
For every record that is entered the time increments my seconds or minutes .
I need to create a new variable Time which should have the value of the first record entered across all observations of the same date.
eg. if 10 records were entered on jan 2 2010 starting at 11.23.23 (time) all 10 records should have the same value of time.When date changes to jan 3 2010 then the first value of time should repeat across observations and so on.
I am stuck in trying to repeat the observation. Can any one please help.

Thanks for your time.

Regards,
Jessica.
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You should consider BY GROUP PROCESSING within a SAS DATA step, while using a RETAIN statement for capturing your first occurence of a "time" within a given "date".

Search the SAS support website http://support.sas.com/ for detailed SAS-hosted documentation and supplemental technical reference material on the topic.

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Hi Jessi,
Following program will help you to resolve your problem.
DATA abs;
INPUT
Name : $8.
Date : date9.
Time : Time5.
;
DATALINES;
abc 27-Jan-01 0:55
abc 27-Jan-01 1:10
abc 27-Jan-01 2:10
abc 27-Jan-01 3:10
bcd 28-Jan-01 4:10
bcd 28-Jan-01 5:10
bcd 28-Jan-01 6:10
bcd 28-Jan-01 7:10
bcd 28-Jan-01 8:10
bcd 28-Jan-01 9:10
bcd 28-Jan-01 10:10
bcd 28-Jan-01 11:10
efg 29-Jan-00 12:10
efg 29-Jan-00 13:10
efg 29-Jan-00 14:10
efg 29-Jan-00 15:10
efg 29-Jan-00 16:10
efg 29-Jan-00 17:10
efg 29-Jan-00 18:10
;

PROC SORT DATA=abs;
BY Name Date;
RUN;

DATA newabs;
RETAIN IniTime;
SET abs;
BY Name Date;
IF FIRST.date THEN
IniTime = Time;

RUN;
Flip
Fluorite | Level 6
Arul;

You commited the carnal sin of first. processing. Not sorting by one variable past what you are looking at the first. of. By moving datalines in your program it no longer works. You are depending on the times being in the correct order before the sort.
Run the below, and you will see the wrong results, you MUST use
BY Name Date Time; to get the correct results all the time.


DATA abs;
INPUT
Name : $8.
Date : date9.
Time : Time5.
;
DATALINES;
abc 27-Jan-01 3:10
abc 27-Jan-01 0:55
abc 27-Jan-01 1:10
abc 27-Jan-01 2:10
bcd 28-Jan-01 4:10
bcd 28-Jan-01 5:10
bcd 28-Jan-01 6:10
bcd 28-Jan-01 7:10
bcd 28-Jan-01 8:10
bcd 28-Jan-01 9:10
bcd 28-Jan-01 10:10
bcd 28-Jan-01 11:10
efg 29-Jan-00 15:10
efg 29-Jan-00 12:10
efg 29-Jan-00 13:10
efg 29-Jan-00 14:10
efg 29-Jan-00 16:10
efg 29-Jan-00 17:10
efg 29-Jan-00 18:10
;

PROC SORT DATA=abs;
BY Name Date ;
RUN;

DATA newabs;
RETAIN IniTime;
SET abs;
BY Name Date ;
IF FIRST.date THEN
IniTime = Time;
RUN;
deleted_user
Not applicable
Thanks FliP for pointing out the mistake...
deleted_user
Not applicable
Thanks Flip for pointing out the mistake...

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 5 replies
  • 1029 views
  • 0 likes
  • 3 in conversation