BookmarkSubscribeRSS Feed
Laura123
Calcite | Level 5

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!

3 REPLIES 3
Reeza
Super User

Look up the retain statement.

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

retain group;

art297
Opal | Level 21

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;

Laura123
Calcite | Level 5

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.

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!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1281 views
  • 8 likes
  • 3 in conversation