Skip to content

Instantly share code, notes, and snippets.

@8bit-pixies
Created November 25, 2012 03:32
Show Gist options
  • Save 8bit-pixies/4142307 to your computer and use it in GitHub Desktop.
Save 8bit-pixies/4142307 to your computer and use it in GitHub Desktop.
002sas
/*
Program: Get all possible Permutations of a known number of words in a string
Author: Chapman Siu
Description:
Using proc plan and proc transpose, a data set is generated which has all possible
permutations.
Then a macro is writen to allow text to be scanned and concatenate to form a new string.
*/
%let permutation = 3; /*change the value of uu to whatever you like*/
data _null_;
fact = fact(&permutation);
call symput('fact',fact);
run;
proc plan;
factors v1 = &fact ordered
u1 = &permutation perm /noprint;
output out = perm_plan;
run;quit;
proc transpose data = perm_plan out = permutation (drop=v1 _NAME_);
by v1;
var u1;
run; /*the resulting data set permutation will generate all possible permutations of words*/
data class1 (keep=name1);
format name1 $4000.;
set sashelp.class;
retain name1;
if mod(_N_,&permutation) = 1 then name1 = name;
else name1 = catx(' ',name1,name);
if mod(_N_,&permutation) = 0 then output;
run; /*this is a sample data set.*/
%macro perm;
data all_perm(keep=name2);
set class1;
format name2 $4000.;
do pt = 1 to last;
set permutation point = pt nobs=last;
name2 = catx(' ',
scan(name1,COL1)
%do i=2 %to &permutation;
,scan(name1,COL&i.)
%end;
);
output;
end;
run;
%mend perm;
%perm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment