Programming the statistical procedures from SAS

longitudinal dataset: assigning a variable value for all records of an individual

Reply
New Contributor
Posts: 2

longitudinal dataset: assigning a variable value for all records of an individual

I have a dataset with multiple records per person (because each record represents a different year). I want to group people in this dataset by a value they had in 1996 with a new variable.

My code is:


group=.;

if year= 1996 and y=0 then group=0;

if year= 1996 and y=1 then group=1;

My problem is that this just assigns a value to my new variable group in 1996 and this new variable has a missing value for all other years. I would like that same value to be assigned for every record of the same individual.

Any suggestions about how to go about this?

Thanks!

Super User
Posts: 18,586

Re: longitudinal dataset: assigning a variable value for all records of an individual

Look up the retain statement.

Use it at the top of your datastep after set and before the if logic.

retain group;

PROC Star
Posts: 7,417

Re: longitudinal dataset: assigning a variable value for all records of an individual

If 1996 is always the first year, the Fareeza's suggestion may be all you need.  Otherwise, the following method will work whether 1996 is first, somewhere else, or not present at all:

data have;

  input id year y;

  cards;

1 1995 0

1 1996 1

1 1997 0

2 1996 0

2 1997 1

2 1998 1

3 1995 1

3 1998 1

;

data want;

  do until (last.id);

    set have;

    by id;

    if year eq 1996 then group=y;

  end;

  do until (last.id);

    set have;

    by id;

    output;

  end;

run;

New Contributor
Posts: 2

Re: longitudinal dataset: assigning a variable value for all records of an individual

Thanks to both of you for your suggestions!  It's true that not all individuals had an observation in 1996, which is something I had not previously considered when approaching this particular problem.

Ask a Question
Discussion stats
  • 3 replies
  • 177 views
  • 8 likes
  • 3 in conversation