Solved
Contributor
Posts: 42

# Extracting the first observation on a daily basis

Hi SAS community :smileygrin:

I'm struggling to find an efficient way to do the following:

Say I have the table

Acc_NrTransaction_DateTransaction_CodeTransaction_Amount
a101JUL2012:06:00:00111500
a101JUL2012:12:32:05111400
a201JUL2012:07:23:23222600
a102JUL2012:05:33:20111100
a201JUL2012:13:43:15111400
a302JUL2012:08:32:10222300
a302JUL2012:09:32:30222500

So the output I want should be in the form of:

Acc_NrTransaction_DateTransaction_CodeTransaction_Amount
a101JUL2012:06:00:00111500
a201JUL2012:07:23:23222600
a102JUL2012:05:33:20111100
a302JUL2012:08:32:10222500

I have tried the following code:

proc sort=old;

by Transaction_Date Acc_Nr;

run;

data wanted;

set old;

by  Transaction_Date;

if first.Acc_Nr then output;

else delete;

run;

But this does not seem to work (Variable first.Acc_Nr is uninitialized) which is understandable.

Do you have an efficient approach to this problem?

Thank you! :smileygrin:

dansaction_Date

Accepted Solutions
Solution
‎03-12-2014 04:18 AM
Super User
Posts: 10,284

## Re: Extracting the first observation on a daily basis

first. and last. are only generated for variables in the "by" list.

Try this:

data int;

set old;

int_day = datepart(Transaction_Date);

run;

proc sort data=int;

by Acc_Nr Transaction_Date;

run;

data wanted (drop=int_day);

set int;

by Acc_Nr int_date;

if first.int_date;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Solution
‎03-12-2014 04:18 AM
Super User
Posts: 10,284

## Re: Extracting the first observation on a daily basis

first. and last. are only generated for variables in the "by" list.

Try this:

data int;

set old;

int_day = datepart(Transaction_Date);

run;

proc sort data=int;

by Acc_Nr Transaction_Date;

run;

data wanted (drop=int_day);

set int;

by Acc_Nr int_date;

if first.int_date;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 42

## Re: Extracting the first observation on a daily basis

```KurtBremser wrote:

first. and last. are only generated for variables in the "by" list.

```

Thank you for clearing this up Kurt.

The code you provided works perfectly :smileygrin: :smileygrin: :smileygrin:...

🔒 This topic is solved and locked.