Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 1.25 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Dynamic variable names in SAS
  2. Index  Var1   Var2  Var3
  3. 1      78.3   54.7  79.8
  4. 3      67.2   56.2  12.3
  5. 2      65.3   45.2  98.1
  6. 1      56.2   49.7  11.3
  7. 1      67.2   98.2  98.6
  8.        
  9. Index  Var1   Var2  Var3  Var_Index
  10.     1      78.3   54.7  79.8  78.3
  11.     3      67.2   56.2  12.3  12.3
  12.     2      65.3   45.2  98.1  45.2
  13.     1      56.2   49.7  11.3  56.2
  14.     1      67.2   98.2  98.6  67.2
  15.        
  16. data have;
  17. input Index  Var1   Var2  Var3;
  18. cards;
  19. 1      78.3   54.7  79.8
  20. 3      67.2   56.2  12.3
  21. 2      65.3   45.2  98.1
  22. 1      56.2   49.7  11.3
  23. 1      67.2   98.2  98.6
  24. ;
  25. run;
  26.  
  27. data want;
  28. set have;
  29. array vars(*) var: ;
  30. var_index=vars(index);
  31. run;
  32.        
  33. data assign_value;
  34.   set have;
  35.  
  36.   if index = 1 then var_index = var1;
  37.   else if index = 2 then var_index = var2;
  38.   else if index = 3 then var_index = var3;
  39. run;
  40.        
  41. data want;
  42.     set have;
  43.     length _var_name $32 _var_num 5;
  44.     array vars(*) var: ;
  45.     /* if you want name of variable */
  46.     _var_name=VNAME(vars(index));
  47.     /* if you want numeric suffix of variable
  48.             and suffix is not same as position of variable in array
  49.           (so that vars(index) is not usable */
  50.     _var_num=INPUT(SUBSTR(_var_name, ANYDIGIT(_var_name)),32.);
  51. run;
  52.        
  53. var_index = vvaluex(compress("var" || put(index, 8.)));