BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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