BookmarkSubscribeRSS Feed
park2039
Calcite | Level 5

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

    

3 REPLIES 3
art297
Opal | Level 21

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

mojerry2
Fluorite | Level 6

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.

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 3 replies
  • 732 views
  • 0 likes
  • 4 in conversation