BookmarkSubscribeRSS Feed
BchBnz
Obsidian | Level 7

Hello everyone,

I hope you are all well.

I am trying to find the quickest way to save the observed value of a variable "pm" at the last "time" for each "id" per "dat" as a variable.

So far I tried this code:

proc sort data=dir.sampler;

by date id time;

run;

data test;

set dir.sampler;

by date id time;

lastpm=last.pm;

run;

But it didn't work with the error "Variable lastpm uninitialized "

so I tried to at least record the last "time";

data test;

set dir.sampler;

by date id time;

lasttime=last.time;

run;

And it didn't work, and the variable lasttime had the value 0 for all the time series.

Any suggestions?

And as always, Thank you!

5 REPLIES 5
RamKumar
Fluorite | Level 6

You can use if clause with last. to derive your variable.

Replace this line lastpm=last.pm; with if last.time then variable=value;

BchBnz
Obsidian | Level 7

Hello RamKumar,

If I understand correctly you are suggesting to write the code like so:

data test;

set dir.sampler;

by date id time;

if last.time then lastpm=pm;

run;

In this case, sas just gives lastpm the value of pm.

What do you reckon the problem is?

Thanks

CTorres
Quartz | Level 8

Try this:

data test (drop=pm);

set dir.sampler;

by date id time;

lastpm=pm;

if last.time;

run;

esjackso
Quartz | Level 8

I my be misunderstanding your need, but if you need all the rows to remain and just retain the value of the last pm value then you will have to do something like:

proc sort data=dir.sampler;

     by date id descending time ; /*places last time row first in the dataset*/

run;

data test;

     set dir.sampler;

     by date id descending time;

     if first,id then lastpm = pm;

     retain lastpm; /*keeps the value until changes at the next id*/

run;

I dont have anything to test this with so this is untested code.

Eric

BchBnz
Obsidian | Level 7

Hello Esjackso,

Thanks for you reply. However, it was retaining pm not the last observed pm.

So, based on your code, I created a new dataset with only last observations using this code:

proc sort data=dir.sampler;

     by date id descending time ;

run;

data test;

     set dir.sampler;

     by date id descending time;

     if first.id then output test;

run;

It seems to work. Thanks for your help.


Cheers!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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