Search code examples
sasdummy-variable

SAS - Create Dummy Variables for All Variables


I have a dataset with X number of categorical variables for a given record. I would like to somehow turn this dataset into a new dataset with dummy variables, but I want to have one command / macro that will take the dataset and make the dummy variables for all variables in the dataset.

I also dont want to specify the name of each variable, because I could have a dataset with 50 variables so it would be too cumbersome to have to specify each variable name.

Lets say I have a table like this, and I want the resulting table, with the above conditions that I want a single command or single macro without specifying each individual variable:

enter image description here


Solution

  • You can use PROC GLMSELECT to generate the design matrix, which is what you are asking for.

    data test;
       input id v1 $ v2 $ v3 $ ;
       datalines;
    1 A A A
    2 B B B
    3 C C C
    4 A B C
    5 B A A
    6 C B A
    ;
    
    proc glmselect data=test outdesign(fullmodel)=test_design noprint ;
       class v1 -- v3;
       model id = v1 -- v3 /selection=none noint;
    run;
    

    You can use the -- to specify all variables between the first and last. Notice I don't have to type v2. So if you know first and the last, you can get want you want easily.