Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:31:45+00:00 2026-06-18T11:31:45+00:00

The original dataset is like: sub month y 1 1 1 1 2 2

  • 0

The original dataset is like:

sub month y
1   1     1
1   2     2
1   3     3
1   5     5 

What I want is to get the previous 3 ys based on month for each subject, if y is missing at that month, then the new variable is . too. Say , for the above example, lag1 is previous y for last month, lag2 is previous y for 2 months ago, the lag3 is so far so forth:

sub month y lag1 lag2 lag3
1   1     1  .    .    .
1   2     2  1    .    .
1   3     3  2    1    .
1   5     5  .    3    2 

The thing is I checked out lag and dif function, but in my case, what I want on lag depends on month and there also are gaps between month, so I can not use the previous one like lag1 function.

Also I need to do this for many subjects too. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:31:46+00:00Added an answer on June 18, 2026 at 11:31 am

    SQL solution:

    data have;
    input sub month y;
    datalines;
    1   1     1
    1   2     2
    1   3     3
    1   5     5 
    ;;;;
    run;
    
    proc sql;
    create table want as
        select H.sub,H.month, H.y, One.y as lag1, Two.y as lag2, Three.y as lag3
        from have H
        left join (select * from have) One on H.sub=One.sub and H.month=One.month+1
        left join (select * from have) Two on H.sub=two.sub and H.month=Two.month+2
        left join (select * from have) Three on H.sub=three.sub and H.month=Three.month+3
    ;
    quit;
    

    Obviously this gets a bit long if you want 36 of them, but it’s not all that complex at least. There are all sorts of other ways to do this. Don’t use LAG, that’s going to be a headache and not really appropriate anyway. A hash table might be more efficient and require less coding, if you’re familiar with the concept of a hash.

    Hash solution:

    data want;
    if _n_ = 1 then do;
     declare hash h(dataset:'have(rename=y=ly)');
     h.defineKey('sub','month');
     h.defineData('ly');
     h.defineDone();
     call missing(sub,month,ly);
    end;
    set have;
    array lags lag1-lag3;
    do prevmonth = month-1 to month-3 by -1;
      if prevmonth le 0 then leave;
      rc=h.find(key:sub,key:prevmonth);
      if rc=0 then lags[month-prevmonth] = ly;
      call missing(ly);
    end;
    run;
    

    This is pretty simple to expand up to 36 [or whatever] – just change the length of the array to array lags lag1-lag36 and the do statement do prevmonth=month-1 to month-36 by -1; The most work you may have to do is arrange things so month works here – either by creating a integer month, or changing the loop criteria to work with month/year or whatnot. You don’t show how your data is specified so can’t help there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ORIGINAL (see UPDATED QUESTION below) I am designing a new laboratory database that tests
Original Problem: I have 3 boxes each containing 200 coins, given that there is
Original Problem In building our projects, I want the mercurial id of each repository
I have a vector that looks like this: dataset <- c(4,7,9,1,10,15,18,19,3,16,10,16,12,22,2,23,16,17) I would like
I have a dataset called EPL2011_12 . I would like to make new a
I have a dataset with an ID and value. For each value I want
I have a dataset that looks something like this: IDnum State Product Consumption 123
I have a set of SQL files that transform my original dataset. Currently, I
Original question: Note: Below plugin pattern based on the official jQuery docs . I'm
original string is a,d,k I want to remove all , and make it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.