I want to combine two data sets by a common date variable. The first data set was produced using proc expand to convert monthly data to quarterly frequency. The second data set has quarterly data and dates. I cannot merge the two files by date because they don't match up in the two files - while the second data set has informat and format of YYQ6. for the date, the first data set has informat of MMDDYY10. and format of YYQC6. for the date. I gather that the specific day of the quarterly data varies by day within a given quarter in the second data set. I've tried reformatting the date to quarter in the first data set without success. I'd appreciate your thoughts on how to merge the two files.
I'm using SAS 9.4 (TS1M6) on an X64_10PRO platform. Thanks for your help.
1. The PDF file is out of bounds, post your code as code (there's an icon for that).
2. The merge happens on the value, the format has no influence.
3. If the values are indeed different for a given quarter, it's better better to align them say on the first day of the quarter.
Otherwise try SQL merging with put(DATE1,yyq.)=put(DATE2,yyq.)
Why post your code as a PDF? In the future please post it directly into your post using a code block.
SAS formats control the appearance but in the MERGE it's using the underlying variable unless you specify the GROUPFORMAT option. Try adding that to your BY statement and then see if it works.
data stocks_A;
set sashelp.stocks;
where stock='IBM';
format date yymmn6.;
*keep only relevant variables for testing;
keep date open;
*rename to identify source;
rename open=OpenA;
run;
data stocks_B;
set sashelp.stocks;
where stock='IBM';
*dates are not the same now in each data set;
DATE = DATE + 1;
format date yymmn6.;
*keep only relevant variables for testing;
keep date open;
*rename to identify source;
rename open=OpenB;
run;
proc sort data=stocks_A; by date;
proc sort data=stocks_B; by date;
run;
*with group format;
data merged;
merge stocks_A stocks_B;
by date groupformat;
format date yymmn6.;
run;
*without group format;
data merged_WRONG;
merge stocks_A stocks_B;
by date;
format date yymmn6.;
run;
1. The PDF file is out of bounds, post your code as code (there's an icon for that).
2. The merge happens on the value, the format has no influence.
3. If the values are indeed different for a given quarter, it's better better to align them say on the first day of the quarter.
Otherwise try SQL merging with put(DATE1,yyq.)=put(DATE2,yyq.)
Thanks for your observations. This was my first post and I'll make sure to post in code in the future. The put statements in SQL worked perfectly.
>This was my first post and I'll make sure to post in code in the future.
No worries, all good. 🙂
I wish there were better explanations and requirements on the post page.
The grey box on the right is much too incomplete, too easily ignored, and posters keep being reminded by repliers about how they should post so they can be helped. @ChrisHemedinger
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.