DATA Step, Macro, Functions and more

How to get a single line

Posts: 0

How to get a single line


Im fairly new to SAS and learning it, I have a dataset like,

obs ID start_dt end_dt

1 9000010130143072000000 20070801 20070831
2 9000010130143072000000 20070901 20070930
3 9000010130143072000000 20071001 20071031
4 9000010130143072000000 20071101 20071130
5 9000010130143072000000 20071201 20071231
6 9000010130143072000000 20080101 20080131
7 9000010130143072000000 20080201 20080229
8 9000010130143072000000 20080301 20080331
9 9000010130143072000000 20080401 20080430
10 9000010130143072000000 20080501 20080531

How can I convert it in a single record as below,

obs ID start_dt end_dt

1 9000010130143072000000 20070801 20080531

New Contributor
Posts: 3

Re: How to get a single line

Your newness to SAS is not the main issue here. You need to specify what rule you want followed to collapse the nine obs you have shown to just one.

I'll guess it is: for all obs with same ID, create one rec with first rec's start_dt and last rec's end_dt.


Before you answer, note I did not say "first rec" by date or by current order in the data - I did not say either. You must include this and every other imaginable detail in your specs.
Super User
Posts: 9,676

Re: How to get a single line

Are these data the whole content of your dataset?
If it is, it is much achieved.
data op;
set yourdataset end=last;
retain _start _obs ;
if _n_ eq 1 then do; _start=start_dt; _obs=obs;end;
if last then do; _end=end_dt; output; end;
keep _obs id _start _end;

If you have a couple of groups of id ,then using '' '' to get it.

It somewhat looks like the previous post in this forum?? Weird!!

Valued Guide
Posts: 2,175

Re: How to get a single line

this is what I like about discussion forums: discovering how many different ways there might be to interpret a question ( let alone the number of ways to program the assumed answer)
Here is it min(start) and max( end) or first.start and last.end
or something else???
Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation