Hi:
The only quibble I have with this approach (of using PUT) is that PUT results in VAR1-VAR3 being created as CHARACTER variables, which can be proved by running a PROC CONTENTS step after the DATA step where the new variables are created using PUT.
This means that
1) EVERY time VAR1-VAR3 are used, SAS will have to convert them from CHARACTER to NUMERIC and
2) if any value of the original variables does not fall into the ranges, AND you stick with the PUT technique, then the out of range CHARACTER value becomes an asterisk '*' as shown in the program below, where one of the original values is 120 -- when the * gets converted to numeric, it will be missing, but, still, that might be unacceptable to an auditor of the program output (for one of the variables to be * and/or for the variable to be CHARACTER when it is being SUMMED).
The program below uses a format and then creates VAR1-VAR4, where one of the variable values is 120 and, thus, gets turned into an * using PUT.
cynthia
[pre]
proc format;
value FmtOne
1 - 25 = 4
26 - 50 = 3
51 - 75 = 2
76 - 100 = 1;
run;
data testform;
infile datalines;
input v1 v2 v3 v4;
format v1-v4 fmtone.;
return;
datalines;
22 27 54 79
23 28 55 80
12 30 60 120
;
run;
ods listing;
proc print data=testform;
title 'what is displayed without formats';
format v1-v4 ;
run;
proc print data=testform;
title 'what is displayed WITH formats';
title2 'internal values are unchanged';
format v1-v4 fmtone.;
run;
data testput; set testform;
var1=put(v1,fmtone.);
var2=put(v2,fmtone.);
var3=put(v3,fmtone.);
var4=put(v4,fmtone.);
Total = var1 + var2 + var3 + var4;
SumTot = sum(var1, var2, var3, var4);
run;
proc contents data=testput;
title 'Note that var1-var4 are CHARACTER variables';
run;
proc print data=testput;
title 'Using PUT statement -- show all ROWS';
run;
proc print data=testput;
title 'What if original v1-v4 number is not in range';
title2 'Then new variable becomes an *';
where var1 = '*' or var2 = '*' or var3 = '*' or var4 = '*';
format v1-v4;
run;
data test_input; set testform;
var1=input(put(v1,fmtone.),best.);
var2=input(put(v2,fmtone.),best.);
var3=input(put(v3,fmtone.),best.);
var4=input(put(v4,fmtone.),best.);
Total = var1 + var2 + var3 + var4;
** show difference between arithemetic with missing and sum stmt with missing;
SumTot = sum(var1, var2, var3, var4);
run;
proc contents data=test_input;
title 'Note that var1-var4 are NUMERIC variables';
run;
proc print data=test_input;
title 'Using PUT statement to make char var and then INPUT to make numeric';
run;
[/pre]
... View more