Help using Base SAS procedures

creating multiple observations from single observation

Reply
Contributor
Posts: 28

creating multiple observations from single observation

I am trying to create multiple oservations from single observation as follows.

Can someone please provide SAS codes?  Thanks.

From:

id                          var1                   yr

000000001                 1                 1997

000000001                 2                 1998

000000001                 3                 1999

000000001                 4                 2002

000000002                 5                 2007

000000003                 6                 2001

000000003                 7                 2003

To:

000000001                 1                 1997

000000001                 2                 1998

000000001                 3                 1999

000000001                 3                 2000

000000001                 3                 2001

000000001                 4                 2002

000000002                 5                 2007

000000003                 6                 2001

000000003                 6                 2002

000000003                 6                 2003

    

PROC Star
Posts: 7,363

creating multiple observations from single observation

Your example to: data doesn't explain what you are trying to accomplish.  However, the following code should provide enough clues to accomplish what you want:

data want (drop=last: v1 year);

  set have (rename=(yr=year var1=v1));

  lastyear=lag(year);

  lastv1=lag(v1);

  if not missing(lastyear) and year ne lastyear+1 then do;

    var1=lastv1;

    do yr=lastyear+1 to year-1;

      output;

    end;

  end;

  var1=v1;

  yr=year;

  output;

run;

HTH,

Art

Contributor
Posts: 28

creating multiple observations from single observation

this is a nice example art297.

To people like me who didn't know the "lag" function, it tells you the value from the record before. This is a nice feature to avoid the "retain" function.

Super User
Posts: 9,681

creating multiple observations from single observation

Or use merge skill mentioned by Peter.C

data temp;
input  id  : $10.   var1                   yr ;
cards;
000000001                 1                 1997
000000001                 2                 1998
000000001                 3                 1999
000000001                 4                 2002
000000002                 5                 2007
000000003                 6                 2001
000000003                 7                 2003
;
run;
data want(drop=_yr i _id);
 merge temp temp(keep=yr id rename=(yr=_yr id=_id) firstobs=2);
 output;
 if _yr-yr gt 1 and id=_id then do;
                     do i=yr+1 to _yr-1;
                     yr=i; output;
                     end;
                     end;
run;
                       

Ksharp

Ask a Question
Discussion stats
  • 3 replies
  • 162 views
  • 0 likes
  • 4 in conversation