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;
proc sort data=tbl;
by id;
run;
data want;
set tbl;
by id;
retain newvar;
if first.id then newvar=z;
run;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.