请选择 进入手机版 | 继续访问电脑版
楼主: huyiustc
3541 13

【100币悬赏】一个SAS查询问题 [推广有奖]

  • 9关注
  • 16粉丝

wizard

教授

32%

还不是VIP/贵宾

-

威望
0
论坛币
7669 个
通用积分
54.6497
学术水平
42 点
热心指数
47 点
信用等级
31 点
经验
9788 点
帖子
502
精华
0
在线时间
1965 小时
注册时间
2011-8-27
最后登录
2023-11-21

huyiustc 发表于 2016-9-13 16:50:09 |显示全部楼层 |坛友微信交流群
相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
如题:
原始数据集含 ID A B三个变量
id   a     b
1  1.1   2
2  1.2   4
3 1.3    4
4  1.0   1
要给每个ID生成一个新变量C,生成原则是,与该ID对应变量a 最接近的两个观测的b的平均
比如 与ID 1的a最接近的是ID 2 和 ID4 那么ID1对应的c=(4+1)/2=2.5由此生成的数据集为
ID   A       B      C
1   1.1    2       2.5
2    1.2   4       3
3    1.3    4   3
4   1.0    1    3
请问各位大侠这个SAScode如何写

二维码

扫码加我 拉你入群

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

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

关键词:SAScode 请问各位大侠 code 原始数据 SASC 如何

回帖推荐

wwang111 发表于4楼  查看完整内容

try this: data test; input id a b; cards; 1 1.1 2 2 1.2 4 3 1.3 4 4 1.0 1 ; data step1; set test; do i=1 to nobs; set test1(drop=id rename=(a=a1 b=b1)) nobs=nobs point=i; val=abs(a-a1); if a ne a1 then output; end; run; proc sort data=step1; by id val; run; data step2; set step1; by id val; if first.id then n=0; n+1; if n

Strive、 发表于5楼  查看完整内容

data aaa; input id a b; cards; 1 1.1 2 2 1.2 4 3 1.3 4 4 1.0 1 ; run; /*数据集拼接*/ proc sql; create table aaa_new as select* from aaa,aaa(rename=(id = id1 a = a1 b = b1)); quit; /*删除ID相同的观测,并求差值*/ data aaa_new; set aaa_new; if id = id1 then delete; diff = abs(a-a1); run; /*排序,为后续做准备*/ proc sort data = aaa_new; by id diff; run; /*让 ...
我是御皇香案吏,谪居犹住在瀛洲
wwang111 发表于 2016-9-13 20:31:46 |显示全部楼层 |坛友微信交流群
请问id=3时,A=1.3,跟1.3最接近的应该是1.1和1.2,对应的B的值是2和4,C的值应该是3,为什么是2.5?

使用道具

huyiustc 发表于 2016-9-13 21:43:51 |显示全部楼层 |坛友微信交流群
wwang111 发表于 2016-9-13 20:31
请问id=3时,A=1.3,跟1.3最接近的应该是1.1和1.2,对应的B的值是2和4,C的值应该是3,为什么是2.5?
抱歉啊,我输错了

使用道具

wwang111 发表于 2016-9-13 22:26:48 |显示全部楼层 |坛友微信交流群
try this:

data test;
input id a  b;
cards;
1  1.1   2
2  1.2   4
3  1.3   4
4  1.0   1
;

data step1;
set test;
do i=1 to nobs;
set test1(drop=id rename=(a=a1 b=b1)) nobs=nobs point=i;
  val=abs(a-a1);
  if a ne a1 then output;
end;
run;

proc sort data=step1;
by id val;
run;

data step2;
set step1;
by id val;
if first.id then n=0;
n+1;
if n<=2;
run;

proc sql;
create table wanted as
select distinct id, a, b, mean(b1) as c
from step2
group by id;
quit;
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
admin_kefu + 30 + 2 + 2 + 2 热心帮助其他会员

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

使用道具

Strive、 发表于 2016-9-13 22:47:16 |显示全部楼层 |坛友微信交流群
data aaa;
        input id a b;
        cards;
        1 1.1 2
        2 1.2 4
        3 1.3 4
        4 1.0 1
        ;
run;
/*数据集拼接*/
proc sql;
        create table aaa_new as
            select* from aaa,aaa(rename=(id = id1 a = a1 b = b1));
quit;
/*删除ID相同的观测,并求差值*/
data aaa_new;
    set aaa_new;
        if id = id1 then delete;
        diff = abs(a-a1);
run;
/*排序,为后续做准备*/
proc sort data = aaa_new;
        by id diff;
run;
/*让每个ID的第一个DIFF为9999,这是为了使第一行变成最小值,方便使用LAG函数*/
data aaa_new1;
    set aaa_new;
        by id;
        if first.id = 1 then diff = 9999;
run;
proc sql;
        select id, a, b, (b1+lag(b1))/2 as c from aaa_new1
                group by id
                having diff = min(diff);
quit;
/*求变量C*/
data aaa_final;
        set aaa_new1;
        c = (b1+lag(b1))/2;
        output;
run;
/*输出结果,在输出窗口*/
proc sql;
        select id, a, b, c from aaa_final
        group by id
        having diff = min(diff);
quit;
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
admin_kefu + 30 + 2 + 2 + 2 热心帮助其他会员

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

使用道具

huyiustc 发表于 2016-9-13 22:54:53 |显示全部楼层 |坛友微信交流群
已经有同学回答了,本帖的悬赏到此结束,但是有大侠有其他思路也欢迎讨论,在本楼前回答的同学我会兑付

使用道具

huyiustc 发表于 2016-9-13 22:55:59 |显示全部楼层 |坛友微信交流群
wwang111 发表于 2016-9-13 22:26
try this:

data test;
烦请上传附件,设置100币,我来购买

使用道具

huyiustc 发表于 2016-9-13 22:56:30 |显示全部楼层 |坛友微信交流群
Strive、 发表于 2016-9-13 22:47
data aaa;
        input id a b;
        cards;
烦请上附件,设置100币我来购买

使用道具

Strive、 发表于 2016-9-13 23:39:36 |显示全部楼层 |坛友微信交流群
已上传!感谢楼主

CODE.zip

670 Bytes

需要: 100 个论坛币  [购买]

代码,谢谢楼主~

本附件包括:

  • CODE.sas

使用道具

huyiustc 发表于 2016-9-13 23:54:34 |显示全部楼层 |坛友微信交流群
Strive、 发表于 2016-9-13 23:39
已上传!感谢楼主
已买,你这个程序的逻辑我很喜欢,好懂

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-16 12:58