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

Hi..

 I have the following table...

 

MacCodeEvent_TypeDateTimeWcodeBcodeLoc
HPUS0001Lathing02JAN1319:24   
HPUS0001Creation29MAY130:007611HTELI1124P1
HPUS0001Creation31JAN140:007402HTELI1015P2
HPUS0001Lathing12JAN157:00   
HPUS000323Lathing13OCT1219:37   
HPUS000323Wheel install25OCT120:00   

 

 

I want to add another row to the above data in SAS EG  ... so that new data looks like:

 

MacCodeEvent_TypeDateTimeWcodeBcodeLoc
HPUS0001Creation01JAN130:00   
HPUS0001Lathing02JAN1319:24   
HPUS0001Creation29MAY130:007611HTELI1124P1
HPUS0001Creation31JAN140:007402HTELI1015P2
HPUS0001Lathing12JAN157:00   
HPUS000323Creation12OCT120:00   
HPUS000323Lathing13OCT1219:37   
HPUS000323Wheel install25OCT120:00   

 

So what is happening here is that first row of each MacCode (Sorted by Date and Time) should always have 'Event' as 'Creation' and 'Date' field should have A DAY BEFORE the date in the next row and Time as 00:00

 

Thanks in Advance....

 

Manoj

SAS beginner

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
proc sort data=have;
by maccode descending date;
run;

data want;
set have;
by maccode;
output;
if last.maccode
then do;
  event_type = 'Creation';
  date = date - 1;
  time = 0;
  wcode = "";
  bcode = "";
  loc = "";
  output;
end;
run;

proc sort data=want;
by maccode date;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User
proc sort data=have;
by maccode descending date;
run;

data want;
set have;
by maccode;
output;
if last.maccode
then do;
  event_type = 'Creation';
  date = date - 1;
  time = 0;
  wcode = "";
  bcode = "";
  loc = "";
  output;
end;
run;

proc sort data=want;
by maccode date;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Am not typing that test data in, so this code is untested (post test data as a datastep).  

The principal is sort the data in descending order, so the first record appears last.  Then a datastep:

data want;
  set have;
  by maccode;
  if last.maccode then do;
    output;
    /* set your replacement values here */
    output;
  end;
  else output;
run;

This outputs all the data, and at the last maccode outputs an additional record.  You can then re-sort the data back to original.  You could also do it with the current sort order, but then you would need to put data into temporary variables, just a bit more fiddly.

Loko
Barite | Level 11

Assuming your data is already sorted:

 

data want;
set have;

by MacCode Date Time;

if first.MacCode then
 do;
	output;
	_MacCode=MacCode;
	_Date=Date-1;
	call missing(of MacCode--Loc);
	MacCode=_MacCode;
	Event_Type='Creation';
	Date=_Date;
	time='0:00't;
	end;
output;


drop _MacCode _Date;
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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 3 replies
  • 1575 views
  • 2 likes
  • 4 in conversation