On Feb 23, 2:20=A0pm, adjgiulio <adjm...@comcast.net> wrote:
> Hi,
>
> I=92ve a dataset where each row includes a unique provider id and 10 $
> variables equal to the sum of charges for a specific procedure code
> (CPT code). The 10 procedure codes variables are named with the
> following convention:
>
> P_5digitsCPT
>
> So, I might have p_90806 and p_40890 and so on. The program id data
> driven so I could end up each time with 10 different codes.
>
> Since all of these start with p_ I can easily define and array using
> the p_: convention.
> Now, I want to define 10 new variables representing percentages for
> each p_ variable (i.e. percent_90806=3Dp_90806/SUM(of p_:).
> The new 10 percentage variables should be named percent_5digitCPT.
> If I could create a second array for the percent_ variables then the
> calculation would be easy.
> But how do I create my percent_ variables to reflect the CPT reported
> in the original 10 p_ variables? I need to extract the CPT code from
> each p_ variable, then create a new variable named using percent_ as a
> prefix and then the CPT code.
> How do I do that?
>
> Thanks,
>
> G
Variable names are compile time thingy, so you can directly
dynamically create new var based on old var.
One way to do it is to get a list of variable using meta data.
Here is another way, use proc transpose:
data have;
array p(5) p_12354 p_23412 p_45323 p_5663 p_32334 (1 3 9 20 8);
do id=3D1 to 2;
output;
end;
run;
data want0;
set have;
length _name_ $30;
array p(5) p_:;
do i=3D1 to 5;
_name_=3Dvname(p(i));
val=3Dp(i);
output;
_name_=3D'pct_'||substr(vname(p(i)),3);
val=3Dp(i)/sum(of p_:);
output;
end;
keep val _name_ id;
run;
proc transpose data=3Dwant0 out=3Dwant (drop=3D_name_);
var val;
id _name_;
by id;
run;
proc print;
run;
pct_ pct_
pct_ pct_
id p_12354 12354 p_23412 23412 p_45323 45323 p_5663 pct_5663
p_32334 32334
1 1 0.024390 3 0.073171 9 0.21951 20
0.48780 8 0.19512
2 1 0.024390 3 0.073171 9 0.21951 20
0.48780 8 0.19512
HTH
Ya