在SAS中重命名重复观察
因此,我一直在试图找出如何在SAS中重命名ID变量(我制作了一个虚拟数据集来尝试此操作,请参见下文) 我需要做的是让所有的在SAS中重命名重复观察,sas,Sas,因此,我一直在试图找出如何在SAS中重命名ID变量(我制作了一个虚拟数据集来尝试此操作,请参见下文) 我需要做的是让所有的a=1,b=2,cd=3等等,但是代码需要可以传输到一个数据集,其中包含30000个观察值,所有观察值都具有不同的ID。我一直在玩first.id和last.id,但毫无用处。有人能帮忙吗 提前谢谢你 编辑 为了澄清,我需要生成输出的代码: a 11 a 2 1 a 3 1 a 4 1 b 3 2 b 5 2 cd 5 3 cd 6 3 cd 1 3 其中第三列是ID变量,
a=1
,b=2
,cd=3
等等,但是代码需要可以传输到一个数据集,其中包含30000个观察值,所有观察值都具有不同的ID。我一直在玩first.id和last.id,但毫无用处。有人能帮忙吗
提前谢谢你
编辑
为了澄清,我需要生成输出的代码:
a 11
a 2 1
a 3 1
a 4 1
b 3 2
b 5 2
cd 5 3
cd 6 3
cd 1 3
其中第三列是ID变量,每个唯一hno值增加一个如果数据按hno排序,则将hno编码为索引。SAS中的重命名通常指变量、数据集等对象
DATA trial;
input hno $ y;
datalines;
a 1
a 2
a 3
a 4
b 3
b 5
cd 5
cd 6
cd 1
;
run;
data trial2;
set trial;
by hno;
if first.hno then id + 1;
run;
proc print;
run;
如果输入未排序,则使用PROC SUMMARY创建并索引ID数据集,并使用键控集添加ID
DATA trial;
input hno $ y @@;
datalines;
a 1 a 2 b 3 b 5 cd 5 cd 6 a 3 a 4
cd 1
;
run;
proc summary nway data=trial;
class hno;
output out=index(drop=_type_ _freq_ rename=(_level_=id) index=(hno)) / levels;
run;
proc print;
run;
data trial2;
set trial;
set index key=hno/unique;
run;
proc print;
run;
如果数据按HNO排序,则将HNO编码为索引。SAS中的重命名通常指变量、数据集等对象
DATA trial;
input hno $ y;
datalines;
a 1
a 2
a 3
a 4
b 3
b 5
cd 5
cd 6
cd 1
;
run;
data trial2;
set trial;
by hno;
if first.hno then id + 1;
run;
proc print;
run;
如果输入未排序,则使用PROC SUMMARY创建并索引ID数据集,并使用键控集添加ID
DATA trial;
input hno $ y @@;
datalines;
a 1 a 2 b 3 b 5 cd 5 cd 6 a 3 a 4
cd 1
;
run;
proc summary nway data=trial;
class hno;
output out=index(drop=_type_ _freq_ rename=(_level_=id) index=(hno)) / levels;
run;
proc print;
run;
data trial2;
set trial;
set index key=hno/unique;
run;
proc print;
run;
您也可以尝试
格式
proc format;
value $ cn
'a' = 1
'b' = 2
'cd' = 3
;
run;
DATA trial;
input hno $ y;
*format hno $cn.;
id = put(hno, $cn.);
datalines;
a 1
a 2
a 3
a 4
b 3
b 5
cd 5
cd 6
cd 1
;
run;
您也可以尝试
格式
proc format;
value $ cn
'a' = 1
'b' = 2
'cd' = 3
;
run;
DATA trial;
input hno $ y;
*format hno $cn.;
id = put(hno, $cn.);
datalines;
a 1
a 2
a 3
a 4
b 3
b 5
cd 5
cd 6
cd 1
;
run;
你的问题不是很清楚。您是否试图为数据集中第一个变量的每个唯一值设置一个序列号?是的,正是这样。这里有一个ID变量,它是由hno变量构成的,每次有一个新的hno值,它会在ID号上加1。我需要数据集在以后看起来像这样:
hno y ID a 1 1 a 2 1 a 3 1 a 4 1 b 3 2 b 5 2 cd 5 3 cd 6 3 cd 1 3
等等。此外,我还需要提前道歉,因为我是这个网站的新手,不知道如何将其转换为可读格式!你的问题不是很清楚。您是否试图为数据集中第一个变量的每个唯一值设置一个序列号?是的,正是这样。这里有一个ID变量,它是由hno变量构成的,每次有一个新的hno值,它会在ID号上加1。我需要数据集在以后看起来像这样:hno y ID a 1 1 a 2 1 a 3 1 a 4 1 b 3 2 b 5 2 cd 5 3 cd 6 3 cd 1 3
等等。此外,我还需要提前道歉,因为我是这个网站的新手,不知道如何将其转换为可读格式!哦,我的话,我不敢相信这是直截了当的!老实说,非常感谢你!!哦,我的话,我不敢相信这是直截了当的!老实说,非常感谢你!!