根据另一个数据集的顺序对数据集进行排序。

huangapple go评论65阅读模式
英文:

Sort a dataset based on the order of another

问题

这是您提供的要翻译的内容:

第一个数据集如下:

>           PATID         Mean           
>           0001          234
>           0004          230
>           0007          237
>           0008          239
>           0010          231
>           ....          ...        

第二个数据集如下:

>           PATID         std           
>           0010          4.1
>           0008          5.9
>           0007          6.8
>           0001          1.1
>           0004          12.9
>           ....          ...  

是否有一种方法可以按照第一个数据集的PATID顺序对第二个数据集进行排序?尽管为简单起见,没有显示所有内容,但这两个数据集具有相同的PATID,尽管顺序不同。

谢谢您的提前帮助!

英文:

I have two datasets that look like this:

> PATID Mean
> 0001 234
> 0004 230
> 0007 237
> 0008 239
> 0010 231
> .... ...

and a second one that looks like this:

> PATID std
> 0010 4.1
> 0008 5.9
> 0007 6.8
> 0001 1.1
> 0004 12.9
> .... ...

Is there a way to sort the second dataset by PATID with the order of PATID of the first? Although not all are shown for simplicity, the two datasets have the same PATIDs although in a different order.

Thank you in advance

答案1

得分: 2

我猜 `PATID` 是一个字符变量,考虑到前导的 0。
一个简单的 `proc sort` 应该就能解决问题

data have;
infile datalines;
input patid $4. std :8.;
datalines;
0010 4.1
0008 5.9
0007 6.8
0001 1.1
0004 12.9
;


proc sort data=have out=want; by patid; quit;


patid std
0001 1.1
0004 12.9
0007 6.8
0008 5.9
0010 4.1


___

如果数据集一没有按升序排序,你可以创建一个序列顺序然后左连接

data have1;
infile datalines;
input patid $4. mean :8.;
datalines;
0001 234
0007 237
0004 230
0010 231
0008 239
;

data have2;
infile datalines;
input patid $4. std :8.;
datalines;
0010 4.1
0008 5.9
0007 6.8
0001 1.1
0004 12.9
;

data have1_view / view=have1_view;
set have1;
seq=n;
run;

proc sql;
create table want as
select t1.patid, t2.std
from have1_view t1 left join have2 t2
on t1.patid = t2.patid
order by seq
;
quit;


patid std
0001 1.1
0007 6.8
0004 12.9
0010 4.1
0008 5.9

英文:

I am guessing PATID is a character variable given the leading 0's.
A simple proc sort should do the trick

data have;
infile datalines;
input patid $4. std :8.;
datalines;
0010 4.1
0008 5.9
0007 6.8
0001 1.1
0004 12.9
;
proc sort data=have out=want; by patid; quit;
patid std
0001  1.1
0004  12.9
0007  6.8
0008  5.9
0010  4.1

Should dataset one not be sorted in an ascending order, you could create the sequence order and left join

data have1;
infile datalines;
input patid $4. mean :8.;
datalines;
0001 234
0007 237
0004 230
0010 231
0008 239
;

data have2;
infile datalines;
input patid $4. std :8.;
datalines;
0010 4.1
0008 5.9
0007 6.8
0001 1.1
0004 12.9
;

data have1_view / view=have1_view;
set have1;
seq=_n_;
run;

proc sql;
	create table want as
	select t1.patid, t2.std
	from have1_view t1 left join have2 t2
	on t1.patid = t2.patid
	order by seq
	;
quit;
patid std
0001  1.1
0007  6.8
0004  12.9
0010  4.1
0008  5.9

huangapple
  • 本文由 发表于 2023年7月27日 20:25:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定