BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PetePatel
Quartz | Level 8

Hi,

 

I have a dataset with variables date, AR and TA2.

 

I want to create AR2 and fix it at the earliest AR date for all subsequent rows (i.e. 2000-01-01). In this case the value should always be 0.020158103.

 

How can I achieve this in the shortest number of steps? I have tried the retain function as well as a sort, delete and merge but this is quite messy.

 

DateARTA2AR2
2000-01-010.020158103 0.020158103
2000-02-010.0186927710.0212584890.020158103
2000-03-010.0213983380.0224329560.020158103
2000-04-01 0.0218812630.020158103
2000-05-01 0.0220856270.020158103
2000-06-01 0.021900270.020158103
2000-07-01 0.0216671150.020158103
2000-08-01 0.0213016720.020158103
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Ok. then this will do

 

data have;
input Date :anydtdte12. AR TA2;
format date yymmdd10.;
infile datalines dlm=',';
datalines;
2000-01-01,0.020158103,0.020158103
2000-02-01,0.018692771,0.021258489
2000-03-01,0.021398338,0.022432956
2000-04-01,           ,0.021881263
2000-05-01,           ,0.022085627
2000-06-01,           ,0.02190027 
2000-07-01,           ,0.021667115
2000-08-01,           ,0.021301672
;

data want;
   set have;
   if _N_ = 1 then AR2 = AR;
   retain AR2;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Does this need to work for by groups as well?

PetePatel
Quartz | Level 8
Hi thanks for your reply draycut. No, it just needs to work for the earliest date which will always be the first observation as the dataset is sorted in ascending date order.
PeterClemmensen
Tourmaline | Level 20

Ok. then this will do

 

data have;
input Date :anydtdte12. AR TA2;
format date yymmdd10.;
infile datalines dlm=',';
datalines;
2000-01-01,0.020158103,0.020158103
2000-02-01,0.018692771,0.021258489
2000-03-01,0.021398338,0.022432956
2000-04-01,           ,0.021881263
2000-05-01,           ,0.022085627
2000-06-01,           ,0.02190027 
2000-07-01,           ,0.021667115
2000-08-01,           ,0.021301672
;

data want;
   set have;
   if _N_ = 1 then AR2 = AR;
   retain AR2;
run;
PetePatel
Quartz | Level 8
Thanks! I tried retain with first.date to cover myself but this works fine.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 3997 views
  • 2 likes
  • 2 in conversation