统计211

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10827|回复: 0
打印 上一主题 下一主题

SAS中ARRAY的应用:哑变量化

[复制链接]
跳转到指定楼层
1
发表于 2010-5-30 12:03:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
【转帖】您好!请教一个问题好吗?
我想把一个数组array a(10) ( 5 4 4 4 4 4 3 2 1 1)排秩,数组中相同值取平均秩次,即想把它排成b(10)(1 4 4 4 4 4 7 8 9.5 9.5)
请问如何编程啊?谢谢指教!


其实这个问题比较好解决,至少算法上。
取每一个数,与其他数比较,如果小于其它数,那么秩次往后走一位,如果相等,那么秩次走0.5位。可以理解不?这个是关键哦。实现的过程并不复杂的。
至于array,即数组,在SAS中还是有广泛的应用,可以方便很多操作。

此题的解答如下:
data a;
array a(10) ( 4,5,4,4 ,4 ,4 ,3 ,1, 2, 1);
array b(10) ;
do i=1 to 10;
b(i)=1;
do j=1 to 10;
  if a(i)=a(j) then
   if i ne j then b(i)=b(i)+0.5;
  if a(i)<a(j) then b(i)=b(i)+1;
end;
end;
run;


其实这个并不难,或者说也不实用。
介绍一个相对实用的,即多分类无序变量的哑变量赋值。
假设变量zhi是一个表示职业的多分类变量,共有1-7个值,分别表示7种职业,现在为了进行回归,必须对其进行哑变量化,那怎么样简单的进行哑变量的设置呢?
data b;
set a;
array zhiye(7);
do i=1 to 7;
if i ne zhi then zhiye(i)=0;
else zhiye(i)=1;

end;
run;

这样会另外产生zhiye1-zhiye7共7个一组的哑变量。当然也可以对这7个哑变量分别命名,是含义更容易理解
array zhiye(7) gongren nongmin xuesheng jiaoshi yisheng shangren junren;
更多的关于array的可以参考英文内容:
What is a SAS array?

A SAS array

* a set of variables grouped together for the duration of a data step by being given a name in an ARRAY statement

array pop(1:5) ga sc nc va wv;

o array name is "pop"
o the sequence of variable names ga sc nc va wv is the "array list"
o the variables in the array list are the "array elements"
o each array element in this example has a position number in the array list from 1 to 5

Convenience of arrays

* two ways to refer to variables which are array elements
o variable name
o array name with subscript pointing to position of variable in the array list

pop(3) refers to the variable in position # 3 in the array pop (nc in above example)


Subscripts can be

* constants
* variables
* expressions

Array elements

* must all be of same type (numeric or character)
* can be variables which exist or which will be created in the data step


One-dimensional array declarations

array x(1:6) a b c d e f; x(5) same as e

array x(0:5) a b c d e f; x(4) same as e

array quiz(20) q1-q20; equivalent array declarations
array quiz(1:20) q1-q20; quiz(4) same as q4
array quiz(*) q1-q20; subscript lower bound=1, upper bound=20

array quiz(20); SAS creates quiz1-quiz20 as array elements

array color(1:3) $ 1 character array, elements have length=1 character
red blue green; color(2) same as blue

array pop(1:5) yr95-yr99; pop(2) same as yr96

array pop(95:99) yr95-yr99; pop(96) same as yr96

array x(*) _numeric_; all numeric variables on the observation
array y(*) _character_; all character variables on the observation
array z(*) _all_; all variables on the observation


Two-dimensional array declarations

array quiz(1:4,1:5) q1-q20; picture 4 rows, 5 columns:
q1 q2 q3 q4 q5
q6 q7 q8 q9 q10
q11 q12 q13 q14 q15
q16 q17 q18 q19 q20

q(2,4) same as q9

array pop(1:3,98:99) nc98 nc99 va98 va99 sc98 sc99; picture 3 rows, 2 columns:
nc98 nc99
va98 va99
sc98 sc99

pop(3,99) same as sc99


Why use SAS arrays?

* repeat an action or set of actions on each of a group of variables
* write shorter programs
* restructure a SAS data set to change the unit of observation


Simple examples using one-dimensional arrays
1. Recode the set of variables A B C D E F G in the same way: if the variable has a value of 99 recode it to SAS missing.

array v(7) a b c d e f g;
do k=1 to 7;
if v(k)= 99 then v(k)=.;
end;


2. Each observation of your data set has five variables SEX1 SEX2 SEX3 SEX4 SEX5 which give the sex (1=male, 2=female) of up to 5 persons. You want to count the number of males (MALES) and the number of females (FEMALES) on each observation.

array sex(1:5) sex1-sex5;
males=0;
females=0;
do i=1 to 5;
if sex(i)=1 then males=males+1;
else if sex(i)=2 then females=females+1;
end;


3. Recode all numeric variables in your data set as follows: if a variable has a value of 98 or 99 recode it to SAS missing.

array nvar(*) _numeric_;
do i=1 to dim(nvar);
if nvar(i)=98 or nvar(i)=99 then nvar(i)=.;
end;
以及附件!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


免责声明|关于我们|小黑屋|联系我们|赞助我们|统计211 ( 闽ICP备09019626号  

GMT+8, 2025-5-6 10:56 , Processed in 0.082146 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表