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

I am trying to run the following code to keep only the last 'CreateDate' for each 'FileNo' however it is returning a blank dataset with the headers only.

date Dates_1;

set Dates;

If Last.createdate then output;

by FileNo;

Run;

Any assistance would be appreciated??

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

I guess you wanted this:

proc sort data=Dates;

by FileNo createdate;

run;

data Dates_1;

set Dates;

by FileNo;

if last.FileNo;

run;

which will give you the latest createdate for any FileNo.

If you take a look at the log of your first try, you will get a clue where your problem is:

NOTE: Variable 'last.createdate'n is uninitialized.

A first. and last. pseudovariable is created only for those columns named in a by statement.

View solution in original post

4 REPLIES 4
Steelers_In_DC
Barite | Level 11

without the by statement first. or last. will not work, simple fix:

date Dates_1;

set Dates;

by createdate; /*add this line, must be sorted by createdate*/

If Last.createdate then output;

Run;

KMW156
Calcite | Level 5

I will try this now.

I assumed i would have to have it sorted by FileNo as i wanted to keep the last CreateDate for each FileNo.
I will post the outcome, although i am sure it will now work if it was a simple error as advised.

Thanks

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Ah, yes, I think you are trying to get the last file date for each file yes?  Mark Johnson has provided the answer, however that will just give one record for the data.  First you want to sort your data by fileno and create date, then:

date Dates_1;

set Dates;

by fileno createdate;

If Last.fileno then output;

Run;

This will give one row per file with the last create date.

You can do it in one step in SQL;

proc sql;

     create table WANT as

     select     FILENO,

                   CREATEDATE

     from       HAVE

     group by FILENO

     having CREATEDATE=max(CREATEDATE);

quit;

Kurt_Bremser
Super User

I guess you wanted this:

proc sort data=Dates;

by FileNo createdate;

run;

data Dates_1;

set Dates;

by FileNo;

if last.FileNo;

run;

which will give you the latest createdate for any FileNo.

If you take a look at the log of your first try, you will get a clue where your problem is:

NOTE: Variable 'last.createdate'n is uninitialized.

A first. and last. pseudovariable is created only for those columns named in a by statement.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 1013 views
  • 0 likes
  • 4 in conversation