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

Hi

I have dataset with ID, datetime, group, colour

 

ID                       datetime                  group            colour

 

Z67897F     26Nov2017:09:26:00          Fiat           white

Z67897F     26Nov2018:12:26:00          Fiat           red

Z67897F     01May2016:12:26:00         Ford          black

Z67897F     01May2016:12:48:00         Ford          purple

D45671Z    26Dec2017:09:26:00          Toyota       yellow

D45671Z    26Dec2017:10:26:00          Toyota       black

D45671Z    26Jan2019:09:26:00          Buick         purple

.

.

Z678B7F     09Jun2018:09:26:00          Lexus           white

 

I can very  easy to click and drag this report in EG. But is a way to get result using data step,

to get for every ID one row with last result for each group and colour

I have many datasets and number of groups differs. Is a way to get as many columns as number of groups?

 

EG

 

ID            Fiat last datetime               colour       ford_last_datetime            colour      Toyota last dateti         colour

Z67897F    26Nov2018:12:26:00        red          01May2016:12:48:00       purple           Null                             Null

D45671Z      Null                                 Null                 Null                           Null           26Dec2017:10:26:00     black

....

1 ACCEPTED SOLUTION

Accepted Solutions
Mark7
Obsidian | Level 7

Something like this?

proc sort data=example;
by Id group DateTime;
run;

 

 

data get_last;
Set example;

by ID group DateTime;
if last.group ;

run;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Is this for some display table, or for perhaps export to Excel?

 

Those would be good reasons, but if you are doing this for further analysis, I think this is a poor format for additional analysis.

--
Paige Miller
Mark7
Obsidian | Level 7

It is only for management, display table. Table contains over 20 mln rows

Reeza
Super User
This will be a very sparse data set...that being said:

1. Use LAST. to keep last record for each ID/GROUP
2. Use PROC TRANPOSE twice to flip to a wide format, once for colour and once for group.
3. Merge the results by ID.
2.
Mark7
Obsidian | Level 7

I am SAS EG drag and drop user ana found a problem to get proper solution  

I prepared dataset

data example;
input ID $ DateTime group $ colour $;
informat DateTime datetime19. ;
format DateTime datetime19. ;
datalines;
Z67897F 11SEP18:06:12:00 Fiat white
Z67897F 26Nov18:12:26:00 Fiat red
Z67897F 01May16:12:26:00 Ford black
Z67897F 01May16:12:48:00 Ford purple
D45671Z 26Dec17:09:26:00 Toyota yellow
D45671Z 26Dec17:10:26:00 Toyota black
D45671Z 26Jan19:09:26:00 Buick purple
Z678B7F 09Jun18:09:26:00 Lexus white
;
run;

 

any hint how I can get 

 

IDDateTimegroupcolour
D45671Z26Jan2019 9:26:00Buickpurple
D45671Z26Dec2017 10:26:00Toyotablack
Z67897F26Nov2018 12:26:00Fiatred
Z67897F01May2016 12:48:00Fordpurple
Z678B7F09Jun2018 9:26:00Lexuswhite
Mark7
Obsidian | Level 7

Something like this?

proc sort data=example;
by Id group DateTime;
run;

 

 

data get_last;
Set example;

by ID group DateTime;
if last.group ;

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