Your SAS programs, embedded in web apps and elsewhere

Comparing dates from 2 different datasets

Reply
Frequent Contributor
Posts: 127

Comparing dates from 2 different datasets

Hello,

Maybe you'll find this question very stupid but how can I compare values of a dataset's variable with all values of another dataset's variable ? This has to be done in a data step and not by using proc sql. Using two different set statements with a loop on the second one could be the solution or it's more simple ?

In fact if I found the value (which is a date) of the first dataset's variable in the second one, then I have to change the value of another variable from the first dataset ...

I hope this is more or less clear :\

Thank you all,
Florent
SAS Super FREQ
Posts: 8,742

Re: Comparing dates from 2 different datasets

Hi,
I'm just having a hard time visualizing how your data are structured or what you need to do. For example...if the data looked like this:
[pre]
data want;
infile datalines;
input code $ datewant : mmddyy10.;
return;
datalines;
code1 11/15/1950
code2 06/30/1949
;
run;

data search;
infile datalines;
input name $ date1 : mmddyy10. date2 : mmddyy10.;
return;
datalines;
lucy 12/05/1951 01/15/1953
ricky 11/15/1950 12/05/1951
fred 12/01/1948 11/15/1950
ethel 06/30/1949 08/12/1952
;
run;

ods listing ;
proc print data=want;
title 'want these';
format datewant mmddyy10.;
run;

proc print data=search;
title 'search here';
format date1 date2 mmddyy10.;
run;

[/pre]

Are you saying that you would check every date field on every obs in the 'Search' dataset to see if DATEWANT was found in either DATE1 or DATE2 ??? Is there a way to fabricate some test data that illustrates your problem a bit better???

Given the above data, I'd be very tempted to use a FORMAT for a lookup, assuming that the dates to lookup was a reasonable sized list.
[pre]

proc format;
value wantdate '15Nov1950'd = 'y'
'30Jun1949'd = 'y'
other='n';
run;

data search;
infile datalines;
input name $ date1 : mmddyy10. date2 : mmddyy10.;
ckdate1 = put(date1,wantdate.);
ckdate2 = put(date2,wantdate.);
return;
datalines;
lucy 12/05/1951 01/15/1953
ricky 11/15/1950 12/05/1951
fred 12/01/1948 11/15/1950
ethel 06/30/1949 08/12/1952
;
run;
[/pre]
And then the proc print of the above file would look like this (if you selected where ckdate1 = 'y' or ckdate2 = 'y'):
[pre]
Obs name date1 date2 ckdate1 ckdate2

2 ricky 11/15/1950 12/05/1951 y n
3 fred 12/01/1948 11/15/1950 n y
4 ethel 06/30/1949 08/12/1952 y n

[/pre]

There are lots of ways to make this automated using SAS to create a format from the "want" data set and using SAS macro variables/programs, but there's no point going farther down this road if your data is significantly different.

cynthia
Frequent Contributor
Posts: 127

[Solved] Comparing dates from 2 different datasets

Hi,

Thank you for your answer. It was a really interesting way to compare variables coming from 2 different datasets.

In fact I had to compare a date with another one (test if greater or not) and my problem was that I had to use it within a macro called in a data step. After a deeper analysis with a colleague, it appeared that the second dataset's dates were just holidays' references so I generated macro variables instead of using a dataset.

So the problem is solved Smiley Happy

Thanks again for your answer,

Florent
Ask a Question
Discussion stats
  • 2 replies
  • 198 views
  • 0 likes
  • 2 in conversation