BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I would like to retain the value of column Z for each customer.

The input data set has  2 columns: ID ,Z

1 10
1 15
1 20
2 50
2 45
2 55
3 40
3 42
3 38

The desired data set will have the following columns ID,Z,First_Z with following values:

1 10 10
1 15 10
1 20 10
2 50 50
2 45 50
2 55 50
3 40 40
3 42 40
3 38 40

What is the way to do it using Retain statement?

Data tbl;
input ID Z;
cards;
1 10
1 15
1 20
2 50
2 45
2 55
3 40
3 42
3 38
;
run;
2 REPLIES 2
Jagadishkatam
Amethyst | Level 16
proc sort data=tbl;
by id;
run;

data want;
set tbl;
by id;
retain newvar;
if first.id then newvar=z;
run;
Thanks,
Jag
PeterClemmensen
Tourmaline | Level 20
Data tbl;
input ID Z;
cards;
1 10
1 15
1 20
2 50
2 45
2 55
3 40
3 42
3 38
;
run;

data want;
   set tbl;
   by ID;
   if first.ID then First_Z=Z;
   retain First_Z;
run;
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 841 views
  • 0 likes
  • 3 in conversation