Last active
November 5, 2021 16:20
-
-
Save statgeek/0225481c2cf232e8b380b3e6c2a8a371 to your computer and use it in GitHub Desktop.
SAS - merge with formatted data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*This example demonstrates how you can merge data with a formatted variable and not have to create a new variable; | |
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment