@CherylA wrote:
Hi, I have a string variable of up to length 24 which represents different scores (from 0-9) for 24 months.
Var = "0000000134564388889..."
I need to create 72 variables that are 1 or 0.
i.e score_1 will be 1 if the first number in the Var string is >=1 and 0 if not.
score_2 will be 1 if the second number in the var string is >=1 and 0 if not,
and so on.
I'll then sum the variables for each month using something like this to get a sum of the observations that were >=1 at each month:
proc summary data = data (keep=score_1-score_24) nway missing ; var score_1-score_22; output out = data_out (drop= _type_) sum= ; run ;
Thanks in advance!
Here is one way.
data junk;
Var = "0000000134564388889";
array score_(24);
do i=1 to length(var);
score_[i] = (input(substr(var,i,1),best.)>1);
end;
drop i;
run;
SAS comparisons will return 1 for true and 0 for false so after pulling the value, converting to numeric with input and substr, the comparison yields what was requested.
But again why 72 variables?
You may find it preferable to reshape the data to have a MONTH Value instead of hiding that information in a variable name. Which would be relatively easy for the example data.
data junk2;
Var = "0000000134564388889";
do month=1 to length(var);
score_ = (input(substr(var,month,1),best.)>1);
output;
end;
drop var;
run;
Your summary would use MONTH as a class or by variable, which would then have the NWAY actually do something.
... View more