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
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;
Question unclear. Supply example data. Like this?
data MATCHED UNMATCHED;
if X1=X2 then output MATCHED; else output UNMATCHED:
run;
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
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.*
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
There are no 10k.FILEDATE that match SRC.FILEDATE.
What would you do with the 10Q? etc?
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;
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,
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!
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.
Ready to level-up your skills? Choose your own adventure.