Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active November 5, 2021 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statgeek/0225481c2cf232e8b380b3e6c2a8a371 to your computer and use it in GitHub Desktop.
Save statgeek/0225481c2cf232e8b380b3e6c2a8a371 to your computer and use it in GitHub Desktop.
SAS - merge with formatted data
*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