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;

 

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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