Skip to content

Instantly share code, notes, and snippets.

@JoostImpink
Created July 1, 2016 17:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoostImpink/dc879c1cddf4e655d95f043cb7c44f40 to your computer and use it in GitHub Desktop.
Save JoostImpink/dc879c1cddf4e655d95f043cb7c44f40 to your computer and use it in GitHub Desktop.
Macro to create dummy variables (non-missing values only)
/* Helper macro to make dummy variables
Note: it is not robust for missing values
*/
%macro createDummyVars(dsin=, var=, newvar=);
proc sql noprint;
select distinct &var. into :mvals separated by '|'
from &dsin.;
/* Number of unique values */
%let mdim=&sqlobs;
quit;
data &dsin.;
set &dsin.;
/* Loop through #unique values */
%do _i=1 %to &mdim.;
/* Grab value */
%let _v = %scan(&mvals., &_i., |);
/* Create dummies */
&newvar.&_v. = (&var. eq &_v.);
%end;
run;
%mend;
/* Create year and industry dummies */
%createDummyVars(dsin=da.b_funda, var=fyear, newvar=yr);
%createDummyVars(dsin=da.b_funda, var=sic2, newvar=ind);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment