Search code examples
sasboxplotsgplot

How to use real numbers/numeric values as variables in SAS?


I would like to know how to use variables that are real numbers as variables name. For instance

data have;
input 
Y     0.133      0.124      0.1242    0.142 ;
datalines;
123  121     214     241   241
431  143     141     241   124
214  124     214     142   241
531  432     134     412   124
243  124     134     134   123
;

proc transpose data=have out=tall (rename=col1=x);
  by y notsorted;
  var /* what should I put here? */;
run;

ods html file='hbox-plot.html';

proc sgplot data=tall;
   hbox x / category=y;
   yaxis type=linear;
run;

ods html close;

Trying to use the real values in var selection in the proc transponse I have got the error: syntax error. My expected output would be box plots, where on the x axis I have information based on the real values above, and on the y axis information on y.


Solution

  • Not really sure what you are trying to achieve but if you want to get rid of that syntax error due to the variable names, the following should work.

    data have;
    input 
    Y "0.133"n "0.124"n "0.1242"n "0.142"n ;
    datalines;
    123  121     214     241   241
    431  143     141     241   124
    214  124     214     142   241
    531  432     134     412   124
    243  124     134     134   123
    ;
    
    proc transpose data=have out=tall(rename=(col1=x _name_=var));
      by y notsorted;
      var "0.133"n "0.124"n "0.1242"n "0.142"n;
    run;
    

    The letter "n" is used to distinguish between a quoted string and a name literal value. You might also want to have a deeper look at the VALIDMEMNAME= option and at Names in the SAS Language.