楼主: fattyclouds
4002 9

变量下标求问 [推广有奖]

  • 0关注
  • 0粉丝

本科生

39%

还不是VIP/贵宾

-

威望
0
论坛币
693 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
586 点
帖子
46
精华
0
在线时间
102 小时
注册时间
2008-5-29
最后登录
2019-4-16

楼主
fattyclouds 发表于 2014-2-11 12:20:56 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
有两个数据,第一个数据结构如下:
obs  x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
1      2  3   5   6   9  9  5   5   4    1
2      2  2   4   7   6  3  5   2   4    11
3      1  6   8   2   1  2  9   6   6    14
4    15  3   2   4   6  0  1   2   5     3
5      2  8   7   6   2  5  5   7   3    2
6      2  3   5   6   9  9  5   5   4    1
7      5  3   4   1   3  4  6   7   3    9
8      6  0   1   6   2  5  1   3   4    6
9      2  3   5   6   9  9  5   5   4    1
10    5   5  1   3   6  2  2   4    7   3
第二个数据集只有一个变量
y
1
3
6
8
y表示下标,意思是只取第一个数据集中的x1 x3 x6 x8这四个变量。

因为这两组数据的维度都不相同,也不知道该怎样合并才好,并且使第一个数据集中只保留x1, x3, x6, x8四个变量。

求高手指点!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:数据结构 高手指点 数据集 求高手 obs

回帖推荐

kunkunred 发表于10楼  查看完整内容

First Proc SQL is fine. It selected all the columns as you can see in the log, but there can't be duplicate column names in a SAS data set, so duplicates were not selected in the second SQL. Here is a solution that might be close to what you want. SAS LOG 6977 proc sql noprint; 6978 select selectedindex into: selectedindex separated by ',' 6979 from index; 6980 %put &selectedind ...

沙发
fattyclouds 发表于 2014-2-11 21:05:21
有一个大概地方没说清楚,抱歉。变量y表示的是动态下标,每次产生的y值都不一样,不是固定的。

藤椅
jzh63 发表于 2014-2-11 21:12:49
fattyclouds 发表于 2014-2-11 21:05
有一个大概地方没说清楚,抱歉。变量y表示的是动态下标,每次产生的y值都不一样,不是固定的。
楼主可以试试用hash table.

板凳
fattyclouds 发表于 2014-2-11 21:33:46
jzh63 发表于 2014-2-11 21:12
楼主可以试试用hash table.
多谢!不知道data step或者array有什么函数可以处理这种从x1,...x_n的多列数据中,挑选出非连续下标的部分变量。
非常感谢!

报纸
hsaotsud 发表于 2014-2-11 22:32:04
先用元数据提出大表的变量名
用if substr(name,2) in (1,3,6,8)筛选出元数据的观测即想要的数据集的变量名(y不多的话直接写,多的话把观测赋给宏变量
然后把筛选好的变量名赋给宏变量
最后在原来大表里keep一下

中间两步可以用字典一步到位

地板
kunkunred 发表于 2014-2-12 00:38:00
Here is a solution. See if it's what you want.

data data;
input   x1 x2 x3 x4 x5 x6 x7 x8 x9 x10;
datalines;
2  3   5   6   9  9  5   5   4    1
2  2   4   7   6  3  5   2   4    11
1  6   8   2   1  2  9   6   6    14
5  3   2   4   6  0  1   2   5     3
2  8   7   6   2  5  5   7   3    2
2  3   5   6   9  9  5   5   4    1
5  3   4   1   3  4  6   7   3    9
6  0   1   6   2  5  1   3   4    6
2  3   5   6   9  9  5   5   4    1
5   5  1   3   6  2  2   4    7   3
;
run;
data index;
input index;
datalines;
1
3
6
8
;
run;
data index;
set index;
selectedindex=compress('x'||index);
run;
proc sql noprint;
select selectedindex into: selectedindex separated by ','
from index;
%put &selectedindex;
create table selecteddata as
select &selectedindex
from data;
quit;
已有 3 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
firefox29 + 1 + 1 + 1 有很大启发!
admin_kefu + 100 热心帮助其他会员
fattyclouds + 1 + 1 + 1 精彩帖子

总评分: 论坛币 + 100  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

7
fattyclouds 发表于 2014-2-12 01:34:43
kunkunred 发表于 2014-2-12 00:38
Here is a solution. See if it's what you want.

data data;
好清楚的codes!!就是我想要的结果!围观大牛

8
fattyclouds 发表于 2014-2-12 01:36:55
hsaotsud 发表于 2014-2-11 22:32
先用元数据提出大表的变量名
用if substr(name,2) in (1,3,6,8)筛选出元数据的观测即想要的数据集的变 ...
多谢!

9
fattyclouds 发表于 2014-2-13 02:01:47
kunkunred 发表于 2014-2-12 00:38
Here is a solution. See if it's what you want.

data data;
我还有一个问题想请教,如果我选择的下标变量y里有重复值,比如说y是1,3,6,6,最后通过proc sql产生的selecteddata里只有x1,x3,x6,第二个重复的x6就没有了。请问我该如何调整,才能显示所有x变量?大概我搜索的关键词不对,在网上也没有找合适的资料。谢谢指点!

10
kunkunred 发表于 2014-2-14 00:23:45
fattyclouds 发表于 2014-2-13 02:01
我还有一个问题想请教,如果我选择的下标变量y里有重复值,比如说y是1,3,6,6,最后通过proc sql产生的 ...
First Proc SQL is fine. It selected all the columns as you can see in the log, but there can't be duplicate column names in a SAS data set, so duplicates were not selected in the second SQL. Here is a solution that might be close to what you want.

SAS LOG
6977   proc sql noprint;
6978   select selectedindex into: selectedindex separated by ','
6979   from index;
6980   %put &selectedindex;
x1,x3,x4,x4,x6,x6,x6
6981   create table selecteddata as
6982   select &selectedindex
6983   from data;
WARNING: Variable x4 already exists on file WORK.SELECTEDDATA.
WARNING: Variable x6 already exists on file WORK.SELECTEDDATA.
WARNING: Variable x6 already exists on file WORK.SELECTEDDATA.
NOTE: Table WORK.SELECTEDDATA created, with 10 rows and 4 columns.
6984   quit;

NEW CODE
data data;
input   x1 x2 x3 x4 x5 x6 x7 x8 x9 x10;
datalines;
2  3   5   6   9  9  5   5   4    1
2  2   4   7   6  3  5   2   4    11
1  6   8   2   1  2  9   6   6    14
5  3   2   4   6  0  1   2   5     3
2  8   7   6   2  5  5   7   3    2
2  3   5   6   9  9  5   5   4    1
5  3   4   1   3  4  6   7   3    9
6  0   1   6   2  5  1   3   4    6
2  3   5   6   9  9  5   5   4    1
5   5  1   3   6  2  2   4    7   3
;
run;
data index;
input index;
datalines;
1
3
6
4
6
4
6
;
run;
data index;
set index;
order+1;
run;
proc sort data=index ;
by index;
run;
data index;
set index;
by index;
selectedindex=compress('x'||index);
if first.index then count=1;
else count+1;
run;

data index;
set index;
length sql $30;
if count=1 then sql=selectedindex;
else sql=selectedindex||' as '||compress(selectedindex||'_'||count);
run;
proc sql noprint;
select sql into: selectedindex separated by ','
from index
order by order;
%put &selectedindex;
create table selecteddata as
select &selectedindex
from data;
quit;

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
fattyclouds + 1 + 1 + 1 精彩帖子 非常感谢!!!!

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 05:44