楼主: lnlhckao123
3277 9

[问答] 500论坛币求请问sql的使用 [推广有奖]

  • 0关注
  • 3粉丝

副教授

62%

还不是VIP/贵宾

-

威望
0
论坛币
14810 个
通用积分
10.6125
学术水平
7 点
热心指数
4 点
信用等级
4 点
经验
12566 点
帖子
754
精华
0
在线时间
405 小时
注册时间
2010-8-21
最后登录
2025-6-7

楼主
lnlhckao123 发表于 2013-5-1 17:58:57 |AI写论文
500论坛币
请问高手,sql的作用是什么?不用一一都说出来,只要说常用的就可以了!谢谢高手!!

最佳答案

yongyitian 查看完整内容

/* 这里是几个简单的例子 */ /* working with one table */ proc sql; create table class as /* 创建, 或复制一个表 */ select * from sashelp.class; quit; proc sql; select * /* 查看表中全部内容 用* */ from class; quit; proc sql; /* 查看表中变量属性 */ describe table class; quit; ...
关键词:500论坛币 0论坛币 sql 论坛币 sql
即使在人大经济论坛这个网络世界,我仍以真诚为基础与我的好友进行交往!

沙发
yongyitian 发表于 2013-5-1 17:58:58
/* 这里是几个简单的例子  */

/* working with one table */

proc sql;
     create table class as           /* 创建, 或复制一个表 */
     select *
     from sashelp.class;
quit;

proc sql;
    select *                             /* 查看表中全部内容 用*   */
    from class;
quit;

proc sql;                               /*  查看表中变量属性      */
     describe table class;
quit;

proc sql;                               /* 用count() 函数 查看表中共有多少条纪录  */
   select count(*)
   from class;
quit;

proc sql;                              /* 查看表中变量的非重复观测值 */
     select distinct name as uniue_name
     from class;
quit;

proc sql;                               /* 查看表中总观测数, 变量的非重复值总数 */
     select count(*) as N, count(distinct name) as n_name
     from class;
quit;

proc sql;                              /* 查看表中部分内容, 并计算新变量  */
    select name, sex, weight/(height*0.01)/(height*0.01) as BMI, weight  
    from class;
quit;

proc sql;                              /* 用 count(), Max() 函数 和 group by */
     select sex,
     count(sex) as count,       /* 查看表中男(M),女(F)各有多少条纪录 */
     max(weight) as Max_weight  /* 和最大重量 */
     from class
     group by sex;                 /* 用 group by 语句分组 */
quit;

proc sql;                              /* 选出满足条件的内容,  */
        select name, sex, weight, height     
        from class
        where weight > 100    /* 在 from 语句后, group by前,使用 where 条件语句 */
        group by sex              /* 用 group by 语句分组 */
        having height < 70     /* 在 group by 后,用 having 条件语句 */
        order by name;          /* 最后用 order语句 排续*/
quit;   

/* working with two or more tables */

/* sample datasets */
data class_a;                         /* having 4 observations */
   input name $ course1;
datalines;
A 100
B 100
C 100
F 100
;
data class_b;              /* having 4 observations */
input name $ course2;
datalines;
A 80
C 80
D 80
G 80
;
data class_c;           /* having 3 observations */
input name $ course3;
datalines;
A 60
F 60
G 60
;

proc sql;                                                     /* cartesian join: 生成多个表中所有行的全部组合 */
   select *                                                    /* 4x4x3=48 行 */
   from class_a as a, class_b as b, class_c;   /* use , seperate tables */
quit;


/* inner join: 从cartesian join生成的表中找出满足指定条件的行 */

proc sql;                                             /* inner join two tables */
   select a.name, a.course1, b.course2   
   from class_a as a, class_b as b       /* use , seperate tables */   
   where a.name=b.name;                 /* use where condition   */
quit;

proc sql;                                          /* inner join three or more tables */
   select a.name, a.course1, b.course2, c.course3      
   from class_a as a, class_b as b, class_c as c
   where a.name=b.name=c.name;          /* use where condition */
quit;


/* outer join, 只限于两个表的合并, 共有三种outer join */
proc sql;                     /* 第一种: left join  */
     select a.*, b.*        /* 选出左表的全部及右表中满足条件的行. 左表中不满足条件的行设为missing */
     from class_a as a    /* left table  */
          left join              /* use LEFT JOIN keyword    */   
          class_b as b      /* right table */
     on a.name = b.name;  /* use ON condition         */     
quit;

proc sql;                              /* 第二种: right join */
     select a.*, b.*                 /* 选出右表的全部及左表中满足条件的行, 右表中不满足条件的行设为missing */

     from class_a as a          /* left table  */  
          right join                  /* use RIGHT JOIN keyword   */
          class_b as b             /* right table */
     on a.name = b.name;  /* use ON condition         */               
quit;

proc sql;                      /* 第三种:  full join */
     select a.*, b.*         /* 选出左右表中满足条件和不满足条件的行, 不满足条件的行设为missing */
     from class_a as a    /* left table */
          full join               /* use FULL JOIN keyword    */
          class_b as b       /* right table */
     on a.name = b.name;  /* use ON condition         */               
quit;


proc sql noprint;         /*  create a macro variable */  
      select count(*) into : nobs   
      from class;
quit;

已有 2 人评分学术水平 热心指数 信用等级 收起 理由
lnlhckao123 + 3 + 3 + 3 精彩帖子
rdwalk + 1 + 1 + 1 热心帮助其他会员

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

藤椅
rdwalk 发表于 2013-5-1 18:16:13
期待高手!!!
sql是大部分数据库的查询语言,通用,不同数据库有细微差别。
在sas中,sql的优势有以下几点:
1、summary functions,可以方便的对数据集中的变量进行去重复观测值、求和、标准差、均值、最大、观测值个数、缺失值个数等操作,如果对观测值进行分组,也可以方便得求出相应组的统计值。虽然方便,但效率不高。这些功能在data步中也可以实现,而且效率高,只是代码可能复杂一些。
2、连接数据视图和数据字典。sas中只有这一种方法能实现对数据字典的操作。
3、创建宏变量。
4、2与3结合往往实现很强大的功能。
纯属个人理解,静待高手指点。

可以查看http://wenku.baidu.com/view/3649b8ea6294dd88d0d26b9e.html

http://yangjunccap.blog.163.com/blog/static/10377239820102423238616/
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
lnlhckao123 + 2 + 2 + 2 热心帮助其他会员
Imasasor + 100 + 100 + 5 + 4 + 4 分析的有道理

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

板凳
guanglei 发表于 2013-5-1 18:38:55
SQL是结构化查询语言的缩写,是用于对关系数据库数据库进行查询维护使用的高级语言。

最为常用的就是Select语句了,用于查询数据库中的数据信息。基本的语法是

select (需要查询的字段名称,或者是数据聚合的方法如average等) from (数据表,可以从单个表也可以来源于多个表)
where 查询条件 还有数据表的连接
group by 合并结果
order by 排序


SQL功能非常强大,而且不同的数据库SQL Server或者Oracle还提供了特殊的查询功能,让使用者能够查询到很复杂的查询
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
lnlhckao123 + 1 + 1 + 1 热心帮助其他会员

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

三人行,必有我师焉;择其善者而从之,其不善者而改之

报纸
webgu 发表于 2013-5-1 18:41:30
我常用的sql:
1. 建表。create table
2. 表与表之间的运算。内,外连接,左,右,全连接。
3. 作简单汇总。 sum(),avg(),count()
4. 产生宏变量。

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
lnlhckao123 + 1 + 1 + 1 热心帮助其他会员

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

SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365

地板
lnlhckao123 发表于 2013-5-1 18:45:26
可否请高手给几个实际的例子,谢谢大家!!
即使在人大经济论坛这个网络世界,我仍以真诚为基础与我的好友进行交往!

7
邓贵大 发表于 2013-5-1 23:17:37
楼主好阔气!
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

8
wangfengxi 发表于 2013-5-2 19:06:54
邓贵大 发表于 2013-5-1 23:17
楼主好阔气!

9
luling2010 发表于 2013-5-2 22:52:21
SAS中的DATA步和PROC SQL 互为补充, 各有各的优点和使用灵活之处,虽然大部分SQL都可以用DATA步来代替,但二者同时使用感觉更顺手。

10
lnlhckao123 发表于 2013-5-3 21:59:46
请问luling2010,同时使用data步与proc sql是什么意思?可否给个例子?谢谢!!
即使在人大经济论坛这个网络世界,我仍以真诚为基础与我的好友进行交往!

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

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