楼主: lam_fukming
1635 6

A question about inserting data in SAS [推广有奖]

  • 3关注
  • 9粉丝

已卖:1921份资源

博士生

20%

还不是VIP/贵宾

-

威望
0
论坛币
6005 个
通用积分
12.2090
学术水平
43 点
热心指数
43 点
信用等级
40 点
经验
94521 点
帖子
324
精华
0
在线时间
25 小时
注册时间
2011-8-29
最后登录
2025-5-7

楼主
lam_fukming 发表于 2012-11-10 13:14:20 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
I have a question about inserting data in SAS.
For example, I have a data set:

data a;
input   n   x;
cards;
1   10                                                                                       
2   11.5                                                                                                                                 
4   12                                                                                                                                    
5   10.6                                                                                                                                 
7   9.8
;
run;

As there is no data for n=3,6, then I want to create the value n=3 and n=6 with coressponding x=(11.5+12)/2 and x=(10.6+9.8)/2. How can I do so?
The final data set should look like this:

n   x
1   10
2   11.5   
3   11.75                                                                                                                              
4   12                                                                                                                                    
5   10.6         
6   10.2                                                                                                                        
7   9.8

二维码

扫码加我 拉你入群

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

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

关键词:question insert About Quest Bout question about

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
wocacawo + 1 + 1 + 1 鼓励积极发帖讨论

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

大道無門,
千差有路,
透得此關,
乾坤獨步﹗

沙发
davil2000 发表于 2012-11-10 18:06:43
data b(drop=x_1 x_2);
retain x_1 x_2;
set a;
output;
if  n in (4,7) then
  do;
    n=n-1;
    x=(x+x_2)/2;
    output;
  end;
x_1=x;
x_2=x_1;
run;
proc sort;
by n;
run;
proc print;
run;
data_b.png

已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
南海游客 + 5 + 5 + 5 very good
大数据之魂 + 100 + 5 + 5 + 5 我很赞同

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

R是万能的,SAS是不可战胜的!

藤椅
cufering 发表于 2012-11-10 23:12:14
楼上高人,学习
个人网站:全站用SAS搭建而成,邮编无忧网,网址是http://www.51youbian.com

板凳
lam_fukming 发表于 2012-11-11 16:24:03
davil2000 发表于 2012-11-10 18:06
data b(drop=x_1 x_2);
retain x_1 x_2;
set a;
Thank a lot! What is the purpose of creating x_2? Why can't I simply use x=(x+x_1)/2?
大道無門,
千差有路,
透得此關,
乾坤獨步﹗

报纸
davil2000 发表于 2012-11-11 16:49:42
lam_fukming 发表于 2012-11-11 16:24
Thank a lot! What is the purpose of creating x_2? Why can't I simply use x=(x+x_1)/2?
pls run the codes below, then you can find your results.

data b;
retain x_1 x_2;
set a;
output;
if  n in (4,7) then
  do;
    n=n-1;
    x=(x+x_2)/2;
    output;
  end;
x_1=x;
x_2=x_1;
run;

proc print;
run;
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
数据分析师3K + 100 + 100 + 5 + 5 + 5 精彩帖子

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

R是万能的,SAS是不可战胜的!

地板
lam_fukming 发表于 2012-11-11 20:24:38
davil2000 发表于 2012-11-11 16:49
pls run the codes below, then you can find your results.

data b;
I use the same method for data with different gaps, but the result is not exactly the same as what I expect.
For example,

data a;                                                                                                                                   
input   n   x;                                                                                                                           
cards;                                                                                                                                    
1   10                                                                                                                                    
2   11.5                                                                                                                                 
4   12                                                                                                                                    
5   10.6                                                                                                                                 
7   9.8                                                                                                                                   
10   10.1                                                                                                                                 
11  12                                                                                                                                    
;                                                                                                                                         
run;                                                                                                                                      

data b;                                                                                                                                   
retain x_1 x_2;                                                                                                                           
set a;                                                                                                                                    
output;                                                                                                                                   
if  _n_ in (3,5) then                                                                                                                     
   do;                                                                                                                                    
    n=n-1;                                                                                                                                
    x=(x+x_2)/2;                                                                                                                          
    output;                                                                                                                              
  end;                                                                                                                                    
if  _n_ in (6) then                                                                                                                       
   do j=1 to 2;                                                                                                                           
    n=n-1;                                                                                                                                
    x=(x_2)*j/3 + x*(3-j)/3;                                                                                                                     
    output;                                                                                                                              
  end;                                                                                                                                    

x_1=x;                                                                                                                                    
x_2=x_1;                                                                                                                                 
run;                                                                                                                                      

proc sort data=b;                                                                                                                        
by n;                                                                                                                                    
run;                                                                                                                                      
proc print;                                                                                                                              
run;

The result is as follow:
1.png

It is different from the expected result for n= 8, 9

n      x
8      9.9
9      10

大道無門,
千差有路,
透得此關,
乾坤獨步﹗

7
pobel 在职认证  发表于 2012-11-12 10:53:40
lam_fukming 发表于 2012-11-11 20:24
I use the same method for data with different gaps, but the result is not exactly the same as what ...
data a;                                                                                                                                   
input   n   x;                                                                                                                           
cards;                                                                                                                                    
1   10                                                                                                                                    
2   11.5                                                                                                                                 
4   12                                                                                                                                    
5   10.6                                                                                                                                 
7   9.8                                                                                                                                   
10   10.1                                                                                                                                 
11  12                                                                                                                                    
;                                                                                                                                         
run;      

data b;
   set a;
   by n;
   output;
   lastn=lag(n); lastx=lag(x);
   if _n_>1 and  n ne lastn+1 then do;
         gapn=n-lastn;
                 gapx=x-lastx;
         do i=1 to gapn-1;
                    n=lastn+i; x=lastx+gapx/gapn*i;
                    output;
                 end;           
      end;
        drop gapn gapx lastn lastx i;
run;

proc sort data=b;                                                                                                                        
by n;                                                                                                                                    
run;                                                                                                                                      
proc print;                                                                                                                              
run;
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
数据分析师3K + 100 + 100 + 5 + 5 + 5 精彩帖子

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

和谐拯救危机

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-5 07:22