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
SAS Super FREQ
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.

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!

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
  • 3 replies
  • 814 views
  • 0 likes
  • 3 in conversation