BookmarkSubscribeRSS Feed
adrian_waw
Calcite | Level 5
Hey everyone,

Im new at SAS so i apologize for asking what i assume is a pretty basic function. I have a few million observations and i cant just edit my csv file so i need to manipulate the data in SAS. Im looking to use a function to make a calculation based on any changes in the data in the first two columns.

I would be grateful if someone can tell me if this is possible or do i have to use the proc sql command. Perhaps a screenshot and my coding will help explain better.

http://img148.imageshack.us/img148/6771/49349620.jpg

Based on the example in the screenshot above, i'm calculating R=log(P/lag(P)).
If you look at the screenshot, R is what i want to achieve, while "Wrong R" is what i get from using the code below.

data example1;
set example2;
by variable date;
r=log(P/lag(P));
run;

The conditions to get R is that every time theres changes in the column Variable and Date, I want the data in R for that field to reset to NA as i cant utilize the pre-existing data point in column P.

I hope that explains it well enough, appreciate any help i can get, thanks in advance!
3 REPLIES 3
Doc_Duke
Rhodochrosite | Level 12
Add

IF first.date THEN r=.;

after the computation of r.
Cynthia_sas
Diamond | Level 26
Hi:
For a better understanding of the LAG function for future use and to understand why Doc's suggestion will work, these are good resources:

http://support.sas.com/kb/24/665.html
http://support.sas.com/resources/papers/proceedings09/055-2009.pdf (look at the explanation for table 2a, page 3 in this paper)
http://support.sas.com/documentation/cdl/en/etsug/63348/HTML/default/viewer.htm#/documentation/cdl/e... (discussion of the pitfalls of the LAG and DIF functions)

cynthia
adrian_waw
Calcite | Level 5
Thanks Doc@Duke for the help, that was exactly what i was looking for and thanks for the links Cynthia@sas.

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

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1589 views
  • 0 likes
  • 3 in conversation