BookmarkSubscribeRSS Feed
aarony
Obsidian | Level 7

i have a dataset.

ticker date            value

a        19991231     1

a        20011231     2

a        20031231     3

 

i have another dataset

ticker date

a         20030101

 

i want to find the nearest earlier date from the first dataset and arrive at the following:

ticker date             value

a         20030101       2

 

could you provide me of your guidance? thx in advance.

 

4 REPLIES 4
Astounding
PROC Star

I'm sure SQL can handle this if you are more inclined in that direction.  But a DATA step can do it as well:

 

data want (drop=value rename=(latest_value=value));

set a_dataset (in=in1)

   another_dataset (in=in2);

by ticker date;

if first.ticker then latest_value=.;

if in1 then latest_value=value;

retain latest_value;

if in2;

run;

aarony
Obsidian | Level 7

dear astounding. thx so much! for ur kind help!

aarony
Obsidian | Level 7

hi astounding, sorry but would u mind helping me once more?

for some reason, i am not getting the value 2. rather i am getting 3.

 

thx much

Astounding
PROC Star

Check that your data sets are mentioned in the proper order:

 

set a_dataset (in=in1)

   another_dataset (in=in2);

 

The first data set should be the one with multiple records per ticker.

 

If that's not the issue, I would need to see a few lines of data that are causing the problem.

 

Also note, if both data sets contain the same date value, this program selects the matching date (not an earlier date).  That can be changed if necessary.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 509 views
  • 0 likes
  • 2 in conversation