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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.