-1

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.

LdM
  • 674
  • 7
  • 23
  • This violates tidy data principles massively and isn't supported for a reason...use variables to store the data and then reference them as needed. What happens if a value changes, you would then need to update all of your code to change it? – Reeza Mar 17 '21 at 21:26
  • Unfortunately I have this data. I cannot change the original data. In case you are interested, I opened a new question on how to manipulate a table using proc transpose (or another approach) to get a specific output. https://stackoverflow.com/questions/65533079/how-to-manipulate-a-table-using-proc-transpose – LdM Mar 18 '21 at 08:49

2 Answers2

2

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.

Kermit
  • 3,112
  • 2
  • 10
  • 34
  • Thanks Kermit. I would like to get similar output: https://stackoverflow.com/questions/66663111/values-as-labels-in-box-plots/66670081?noredirect=1#comment117864525_66670081 . Instead of x1 -x4, I have numeric values. Unfortunately on the x axis I can see probably approximation rather than the values I mentioned on the post, making it difficult to understand the chart – LdM Mar 17 '21 at 19:39
2

Don't put data into names. Instead of trying to make a variable named 0.133 put the value of 0.133 into a variable.

The other way to represent a grid of data is to have one observation for each cell with three variables. One for the vertical index, one for the horizontal index and the third to have the value stored in the cell at the intersection of the two.

data have;
  input y @;
  do x=0.133,0.124,0.1242,0.142 ;
    input z @;
    output;
  end;
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
;
Tom
  • 47,574
  • 2
  • 16
  • 29