Help using Base SAS procedures

New at SAS, Is it possible to recalculate values based on 2 conditions?

Reply
Occasional Contributor
Posts: 7

New at SAS, Is it possible to recalculate values based on 2 conditions?

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!
Trusted Advisor
Posts: 2,115

Re: New at SAS, Is it possible to recalculate values based on 2 conditions?

Posted in reply to adrian_waw
Add

IF first.date THEN r=.;

after the computation of r.
SAS Super FREQ
Posts: 8,864

Re: New at SAS, Is it possible to recalculate values based on 2 conditions?

Posted in reply to adrian_waw
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
Occasional Contributor
Posts: 7

Re: New at SAS, Is it possible to recalculate values based on 2 conditions?

Posted in reply to adrian_waw
Thanks Doc@Duke for the help, that was exactly what i was looking for and thanks for the links Cynthia@sas.
Ask a Question
Discussion stats
  • 3 replies
  • 132 views
  • 0 likes
  • 3 in conversation