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

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Anything wrong with the codes?

Options

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 11-27-2012 11:25 PM
(904 views)

Hi All,

I want to generate one random walk sequence, but can't achieve it with the codes below. Anything wrong with the codes? Any saser could help me out? Thanks in advance..

data want;

call streaminit(123);

do i=1 to 1000;

x=rand('normal', 0, 1);

if i=1 then y=x;

else y=lag(y)+x;

output;

end;

run;

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

You are using LAG() with a conditon which usually is wrong.

You should split it into two different data step.

data x; call streaminit(123); do i=1 to 1000; x=rand('normal', 0, 1); output; end; run; data want; set x; y=lag(x)+x; if i=1 then y=x; run;

Ksharp

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Your comment of using lag() conditionally is absolutely appropriate, and I think you are reading OP's mind correctly. However, just in case that OP wants to use lag() on the fly (y=lag(y)), and to avoid conditional lag() as well as 50% obs being missing, we can try:

data want1;

call streaminit(123);

do i=1 to 1000;

x=rand('normal', 0, 1);

y=ifn(i<3,x,lag(y)+x);

/*if i=1 then y=x;*/

/* else y=lag(y)+x;*/

output;

end;

run;

Haikuo

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Or RETAIN may be appropriate:

data want;

call streaminit(123);

retain y .;

do i=1 to 1000;

x=rand('normal', 0, 1);

if i=1 then y=x;

else y=y+x;

output;

end;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I may be wrong here, but it seems to me that the easiest way to fix this is to remove the lag function since it is not needed. Since the overall implicit data loop runs only once, won't the following code generate the desired random walk?

data want;

call streaminit(123);

do i=1 to 1000;

x=rand('normal', 0, 1);

if i=1 then y=x;

else y=y+x;

output;

end;

run;

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.