问题
I have a strange data set and I am hoping you all can help me. I have a data set of the levels of certain environmental contaminants which are measured multiple ways along with the limit of detection are present in a group of research participants. I need these in a wide format, but unfortunately they are currently long and the naming conventions don’t easily translate.
This is what it looks like now:
ID Class Name Weight Amount_lipids Amount_plasma LOD
1 AAA Lead 1.55 44.0 10.0 5.00
1 AAB Mercury 1.55 222.0 100.0 75.00
2 AAA Lead 1.25 25.5 12.0 5.00
I have tried various forms of Proc Transpose with no luck and this seems to be more complex than what specifying a prefix can handle.
I want it to look like this:
ID Weight Lead_lip Lead_plas Lead_LOD Mercury_lip Mercury_plas Mercury_LOD
1 1.55 44.0 10.0 5.0 222.0 100.0 75.0
2 1.25 25.5 12.0 5.0 . . .
I tried a two step transpose process but received the following error ERROR: The ID value "xxxxxxxxxxxx" occurs twice in the same BY group
by id weight name;
run;
proc transpose data=want_intermediate out=want;
by id weight;
id name _name_;
run;
回答1:
You likely have a record with the same ID and weight so it's duplicated. You can add a counter for each ID record and use that. This is a double wide transpose, and it looks like your code was cut off. So to add an enumerator for each ID:
data temp;
set have;
by id;
if first.id then count=1;
else count+1;
run;
Then modify your PROC TRANSPOSE to use ID and count in the BY statement.
来源:https://stackoverflow.com/questions/56877279/error-the-id-value-xxxxxxxxxxxx-occurs-twice-in-the-same-by-group-when-trans