turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- How to calculate new variable n using the new vari...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-01-2016 07:54 PM - edited 05-01-2016 08:05 PM

Hi guys,

I'm trying to calculate a new variable WANT(n), based on the previous observation's variable WANT(n-1).

For example, if n=1, WANT=HAVE/2;

else if n>1 then WANT(n)=WANT(n-1)*0.75+HAVE/2;

I have no idea which code to start. Could anyone give me some idea? Thanks in advance.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to OceanDream

05-01-2016 08:17 PM

I'm not sure if your subject (geodesic distance) is what you meant in your question. If so:

Check out this communities article: How to calculate geodesic distance in SAS by @MikeZdeb!

But it sounds like you need an approach that uses the LAG function (to store the value from a current obs to calculate a new value in the next record). There are lots of papers and articles about this -- here is one.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ChrisHemedinger

05-01-2016 08:24 PM

Chris,

Thank you for your reply. I changed my title. It was automatically filled in by sas when I typed my title.

My question is not only a LAG function question. I think it's more of a LOOP function question, but I don't know how to use it, since the lagged variable I need to use to construct my new variable also need to be constructed the same way, because it's the same new variable, if this makes sense.

Regards,

Haimeng

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to OceanDream

05-01-2016 08:35 PM - edited 05-01-2016 08:35 PM

I think that the implicit loop in the DATA step takes care of this for you. Here's a simple example.

```
data test;
set sashelp.class(rename=(weight=have) keep=weight);
prev_have=lag(have);
if _n_ = 1 then want=have/2;
else want=prev_have*0.75 + have/2;
run;
```

Run that and check the math to see if it's what you want. Note that you must always load up prev_have using the lag function, unconditionally . That makes the prev obs value available for calculation in the next iteration.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to OceanDream

05-01-2016 08:36 PM

Please provide a sample data set. Based on your requirement this may be you are looking for

data have;

input x;

datalines;

2

4

6

8

;

data want;

set have;

y=lag(x)*0.75+x/2;

if _n_=1 then y=x/2;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to stat_sas

05-01-2016 10:43 PM

stat_sas, what I need is Y=lag(Y)*0.75+x/2, not y=lag(x)*0.75+x/2. That's my problem

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to OceanDream

05-01-2016 10:47 PM

Post sample data.

However, I think all you need is a retain.

Add

```
retain y;
***rest of code;
y=y*0.75+x/2;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to OceanDream

05-02-2016 01:23 AM

As mentioned earlier by Reeza, retain statement will be required to get previous value of y for calcuation. Please try the following code:

data want;

set have;

retain y;

if _n_=1 then y=x/2;

else y=y*0.75+x/2;

run;