BookmarkSubscribeRSS Feed
LineMoon
Lapis Lazuli | Level 10

Hello experts,

Please, I would like to have the results of lag by retain function only[ i.e: w_1=lag(w), ...]

I want to use only "retain"

w w_1 w_2 w_3
1      
2 1    
3 2 1  
4 3 2 1
5 4 3 2
6 5 4 3
7 6 5 4
8 7 6 5
9 8 7 6
10 9 8 7
11 10 9 8
12 11 10 9
13 12 11 10
14 13 12 11
15 14 13 12
16 15 14 13
4 REPLIES 4
Reeza
Super User

I don't think that's how RETAIN works. 

 

What have you tried and why do you think RETAIN should be the solution?

mkeintz
PROC Star
  1. Read in an observation.

  2. Make an explicit output of an observation

  3. followed by updating of the retained variables.

 

Now use this technique to experiement just with W and W_1.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Astounding
PROC Star

If your data is contained in a SAS data set, you can take advantage of the fact that all such variables are automatically retained.  Assuming that you are working with numeric variables, you could use:

 

data want;

w_1=w;

set have;

run;

 

If you want to lag for a few observations, you have to explicitly retain those variables that are not coming from a SAS data set.  For example:

 

data want;

w_3 = w_2;

w_2 = w_1;

w_1 = w;

set have;

retain w_1 w_2;

run;

 

The trick is to copy values before the SET statement replaces W.

LineMoon
Lapis Lazuli | Level 10

@all: Thank you very much

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 2446 views
  • 1 like
  • 4 in conversation