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

Hi

 

I am trying to find out whether the dates for two separate variables are the same. For example, I want the output to show if the dates for var='X1' and var='X2' are the same (if date of X1 = date of X2). Otherwise, non-matching dates to be in separate output file.

 

Using SAS v. 9.4

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@VRKiwi 

You would help us to help you if you provide sample data in the form of a working data step (like done below in the code part creating data set have) and then show us the desired/expected result.

Below code implementing the logic I'm guessing you're after.

data have;
  infile datalines dlm=' ' truncover;
  input Obs src $ filedate :yymmdd8.;
  format filedate yymmdd10.;
  datalines;
1 NW 20070919
2 10Q 20080108
3 NW 20071218
4 10Q 20080404
5 NW 20080318
6 10K 20080711
7 NW 20080709
8 10Q 20080923
9 8K 20080917
10 NW 20080917
11 10Q 20081223
12 NW 20081216
13 10Q 20090323
14 NW 20090317
15 10K 20090716
16 NW 20090714
17 10Q 20090925
18 NW 20090922
19 10Q 20091221
20 NW 20091215
;
run;

proc sort data=have;
  by filedate obs;
run;

data unique_date multi_date;
  set have;
  by filedate;
  if first.filedate and last.filedate then output unique_date;
  else output multi_date;
run;

 

View solution in original post

13 REPLIES 13
ChrisNZ
Tourmaline | Level 20

Question unclear. Supply example data. Like this?

data MATCHED UNMATCHED;
  if X1=X2 then output MATCHED; else output UNMATCHED:
 run;

 

Nkhaledi
Calcite | Level 5

The variable (SRC)  takes several values, X1, X2, X3, etc. and another variable (fdate) that has different date values per observation. If fdate of X1 equals fdate of X2 then have both in matched file. Otherwise,  variables (X1 and X2) with unmatched fdate to be in a separate output file. 

 

Thank you

ChrisNZ
Tourmaline | Level 20

You make it really hard to help you. 

We don't even know if you have one or several source tables.

*Please supply example data.*

 

 

Cynthia_sas
Diamond | Level 26
Ditto on the fact that it is hard to help the original poster because of no code or no data. Please see the section of this paper on page 5 that discusses How to Ask a Good Question https://support.sas.com/resources/papers/proceedings12/189-2012.pdf .

Cynthia
Nkhaledi
Calcite | Level 5
Sample data:
Obs src filedate

1 NW 20070919
2 10Q 20080108
3 NW 20071218
4 10Q 20080404
5 NW 20080318
6 10K 20080711
7 NW 20080709
8 10Q 20080923
9 8K 20080917
10 NW 20080917
11 10Q 20081223
12 NW 20081216
13 10Q 20090323
14 NW 20090317
15 10K 20090716
16 NW 20090714
17 10Q 20090925
18 NW 20090922
19 10Q 20091221
20 NW 20091215


Thank you.
If filedate of src='10K' and filedate of src='NW' are the same then output in a matched file. Otherwise, output in unmatched file.
Cynthia_sas
Diamond | Level 26

Hi:
This is partly helpful. However, you do not indicate whether OBS is part of the original data or not. The other thing you don't say is whether your date field is character or numberic. What is the max length of the SRC variable? 3 characters max or 5 characters?

If you are going to create 2 data sets with your conditional logic, then you'll need to use 2 OUTPUT statements in your program. Something like this:

data males females;
  set sashelp.class;
  if sex = 'M' then output males;
  else if sex = 'F' then output females;
run;
 
proc print data=males;
  title 'WORK.MALES';
run;
 
proc print data=females;
  title 'WORK.FEMALES';
run;



Cynthia

Nkhaledi
Calcite | Level 5
Thank you Cynthia,

OBS is part of the original data, date field is numeric, SRC is 3 characters.

Your code ignores the date value. For example, if both male & female are born in the same day, then output matched, else output unmatched.
Cynthia_sas
Diamond | Level 26
:Hi, you are correct, I used an existing dataset, SASHELP.CLASS which does not have a DATE variable. However, in general the syntax will use the same concepts just different variables and conditions.

Cynthia
VRKiwi
Obsidian | Level 7

There are no 10k.FILEDATE that match SRC.FILEDATE.

What would you do with the 10Q? etc?

 

Patrick
Opal | Level 21

@VRKiwi 

You would help us to help you if you provide sample data in the form of a working data step (like done below in the code part creating data set have) and then show us the desired/expected result.

Below code implementing the logic I'm guessing you're after.

data have;
  infile datalines dlm=' ' truncover;
  input Obs src $ filedate :yymmdd8.;
  format filedate yymmdd10.;
  datalines;
1 NW 20070919
2 10Q 20080108
3 NW 20071218
4 10Q 20080404
5 NW 20080318
6 10K 20080711
7 NW 20080709
8 10Q 20080923
9 8K 20080917
10 NW 20080917
11 10Q 20081223
12 NW 20081216
13 10Q 20090323
14 NW 20090317
15 10K 20090716
16 NW 20090714
17 10Q 20090925
18 NW 20090922
19 10Q 20091221
20 NW 20091215
;
run;

proc sort data=have;
  by filedate obs;
run;

data unique_date multi_date;
  set have;
  by filedate;
  if first.filedate and last.filedate then output unique_date;
  else output multi_date;
run;

 

Nkhaledi
Calcite | Level 5
Hi VRKiwi

If there is no match, then you output the unmatched in a different output file.
andreas_lds
Jade | Level 19
And now please provide the expected result of the data you posted.
Nkhaledi
Calcite | Level 5

Hi Andreas, 

 

The result is below:

 

Obs  src   filedate

1 QTRLY_RPT 2011-01-01
2 AR 2011-01-01
3 10Q 2011-01-03
4 10Q 2011-01-03
5 10K 2011-01-03
6 QTRLY_RPT 2011-01-03
7 10Q 2011-01-03
8 QTRLY_RPT 2011-01-03
9 10K 2011-01-03
10 20F 2011-01-03
11 10Q 2011-01-03
12 NW 2011-01-03
13 10K 2011-01-03
14 QTRLY_RPT 2011-01-03
15 NW 2011-01-03
16 20F 2011-01-03
17 10K/A 2011-01-03
18 10K/A 2011-01-03
19 10K 2011-01-03
20 10K 2011-01-03
21 10Q 2011-01-03

 

Please note that this is from the whole sample I have (could be different from the one in the earlier post) and you see various SRC types in the output. The code that Patrick provided matched the SRC types that have the same file dates. 

 

Thank you all,

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 13 replies
  • 2953 views
  • 2 likes
  • 6 in conversation