具有重复 ID 值的 proc 转置

发布于 2024-11-29 07:32:56 字数 607 浏览 2 评论 0原文

我需要 SAS 中 proc 转置过程的帮助。我的代码最初是:

proc transpose data=temp out=temp1; 
by patid;
var text;
Id datanumber;
run;

这给了我错误“ID 值“”在同一个 BY 组中出现两次”。我将代码修改为:

proc sort data = temp; 
by patid text datanumber; 
run;

data temp; 
set temp by patid text datanumber; 
if first.datanunmber then n = 0; 
n+1; 
run;

proc sort data = temp; 
by patid text datanumber n; 
run;

proc transpose out=temp1 (drop=n) let;
by patid;
var text;
id datanumber;
run;

这给了我错误:变量 n 无法识别。添加 let 选项会产生很多错误“在同一个 BY 组中出现两次”。我想保留所有 id 值。

请帮助我。

数据示例: 耐心的文本

I need help with proc transpose procedure in SAS. My code initially was:

proc transpose data=temp out=temp1; 
by patid;
var text;
Id datanumber;
run;

This gave me error "The ID value " " occurs twice in the same BY group". I modified the code to this:

proc sort data = temp; 
by patid text datanumber; 
run;

data temp; 
set temp by patid text datanumber; 
if first.datanunmber then n = 0; 
n+1; 
run;

proc sort data = temp; 
by patid text datanumber n; 
run;

proc transpose out=temp1 (drop=n) let;
by patid;
var text;
id datanumber;
run;

This is giving me error: variable n is not recognized. Adding a let option is giving a lot of error "occurs twice in the same BY group". I want to keep all id values.

Please help me in this.

Data Example:
Patid Text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

物价感观 2024-12-06 07:32:56

当您收到该错误时,它告诉您您尝试创建的一个或多个变量有多个数据点。如果您将“let”添加到 proc 转置行,SAS 可以强制转置并删除额外的数据点。

When you get that error it is telling you that you have multiple data points for one or more variables that you are trying to create. SAS can force the transpose and delete the extra datapoints if you add "let" to the proc transpose line.

故笙诉离歌 2024-12-06 07:32:56

您的数据可能不是唯一的?我创建了一个数据集(具有 patid 和 datanumber 的唯一值)并且转置有效:

data temp (drop=x y);
do x=1 to 4;
    PATID='PATID'||left(x);
    do y=1 to 3;
        DATANUMBER='DATA'||left(y);
        TEXT='TEXT'||left(x*y);
        output;
    end;
end;
proc sort; by _all_; 
proc transpose out=temp2 (drop=_name_);
     by patid;
     var text;
     id datanumber;
run;

我的建议是忘记“n”修复并专注于使 patid 和 datanumber 的数据唯一,一个肮脏的方法是:

proc sort data = temp nodupkey; 
by patid datanumber; 
run;

在开始时你的代码..

Your data is possibly not unique? I created a dataset (with unique values of patid and datanumber) and the transpose works:

data temp (drop=x y);
do x=1 to 4;
    PATID='PATID'||left(x);
    do y=1 to 3;
        DATANUMBER='DATA'||left(y);
        TEXT='TEXT'||left(x*y);
        output;
    end;
end;
proc sort; by _all_; 
proc transpose out=temp2 (drop=_name_);
     by patid;
     var text;
     id datanumber;
run;

my recommendation would be to forget the 'n' fix and focus on making the data unique for patid and datanumber, a dirty approach would be:

proc sort data = temp nodupkey; 
by patid datanumber; 
run;

at the start of your code..

书间行客 2024-12-06 07:32:56

尝试按 patid 文本 n 数据编号(n 在数据编号之前)对数据集进行排序。

Try to sort your dataset by patid text n datanumber, (n before datanumber).

情域 2024-12-06 07:32:56

尝试按 patid n datanumber(n 在 datanumber 之前)对数据集进行排序。和 proc 转置“by patib n”;

Try to sort your dataset by patid n datanumber, (n before datanumber). and proc transpose "by patib n ";

蓝戈者 2024-12-06 07:32:56

如果数据不唯一,另一种解决方法是添加行号字段,以便有一个可以排序和分组的唯一标识符。

Another way to address, if data is not unique, is to add a row number field so that there is a unique identifier you can sort and group by.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文