s******o 发帖数: 656 | 1 各位帮忙看看,多谢多谢!
下边的例子是从我的数据里摘出来的一部分,有四个vairiables:person,year,还有
x和y。
其中x有两个值0和1,y有两个值0和-1,并且x是1的时候y不可能是-1只能是0,y是-1的
时候x不会是1.
Data one;
Input person year x y;
datalines;
1 1990 0 0
1 1991 1 0
1 1992 0 0
1 1993 1 0
1 1994 0 0
1 1995 1 0
1 1996 0 -1
1 1997 1 0
1 1998 0 -1
1 1999 0 -1
2 1991 1 0
2 1992 0 0
2 1993 0 0
2 1994 1 0
2 1995 1 0
2 1996 0 -1
2 1997 1 0
2 1998 0 -1
2 1999 0 -1
;
Run;
我想要做的是:新建一个variable:z,设置为0,整个data按person按year排序后,对
每一个person从早的年份(year)往下看,**如果x出现两次1而且两次1的中间y不能出
现-1,则从第一次出现1那... 阅读全帖 |
|
s******o 发帖数: 656 | 2 这个code对每个person里至少有两个x是1的情况适用,但是如果有一个person只有一个
x是1,那么这个person对应的z就都是0,这个code给出的结果是z都是1比方说
Data one;
Input person year x y;
datalines;
1 1990 0 0
1 1991 1 0
1 1992 0 0
1 1993 1 0
1 1994 0 0
1 1995 1 0
1 1996 0 -1
1 1997 1 0
1 1998 0 -1
1 1999 0 -1
2 1991 1 0
2 1992 0 0
2 1993 0 0
2 1994 1 0
2 1995 1 0
2 1996 0 -1
2 1997 1 0
2 1998 0 -1
2 1999 0 -1
3 1991 1 0
3 1992 0 0
3 1993 0 0
3 1994 0 0
3 1995 0 0
3 1996 0 0
3 1997 0 0
3 1998 0 0
3 1999 0 0
;
Run;
data two;
set one;
by person;
if fi... 阅读全帖 |
|
A****t 发帖数: 141 | 3 用macro会方便一点
data _null_;
length filename $ 10;
input filename $;
call symput('y'||left(_n_), filename);
datalines;
a_20120101
a_20120102
a_20120105
a_20120106
b_20120107
;
run;
%put &y1 &y2 &y3 &y4 &y5; |
|
z*******4 发帖数: 8 | 4 data date;
input x : mmddyy10.;
datalines;
01/01/2012
;
data date;
set date;
y=substr(put(x, date9.),3);
run;
proc print data=date;
format x date9.;
run; |
|
j******o 发帖数: 127 | 5 第一个月的starting信息缺失?Ending和starting之间的关系缺失?用do loop应该可
以得到你要的,试试下面的思路:
data have;
input customer account;
datalines;
32 190
45 260
;
run;
%let mth=15;
data one;
set have;
do i=1 to &mth;
month=i;
if month=1 then do; starting=1; ending=starting*1.25; end;
else do;starting=ending; ending=starting*1.25;end;
output;
end;
drop i;
run;
|
|
G********r 发帖数: 3161 | 6 两个Datasets啊,原数据就是SET语句里的那个,这个Dataset已经有一个Jobcode的变
量,长度是5。你用它来产生新的Dataset,然后改长度成12,但是这个步骤是不能改变
长度的。你自己试试这个Code不就完了。呵呵。
data test;
input jobcode $ 1-5;
datalines;
ABCDE
;
run;
data test2;
set test;
length jobcode $ 12;
run; |
|
w********y 发帖数: 371 | 7 I change your program to:
data test;
input jobcode $ 1-10;
datalines;
ABCDEfghij
;
run;
data test2;
set test;
length jobcode $ 5;
run;
proc print data=test2;
run;
the result is : ABCDEfghij
then what's the point of this length statement?? |
|
s******r 发帖数: 1524 | 8 It looks like your file has problem.
data test;
input @1 date mmddyy10.;
if date='01jan2000'd then event='january 1st';
datalines;
01012000
;
run;
no error. |
|
s******r 发帖数: 1524 | 9 俺希望能filter结果based on macro variable.怎么让下面的code work. 我用macro
function 把varl_lss 改成 “month","id". 但是不work. 各位老大帮忙。
data ttt;
input x $10.;
datalines;
month
id
salary
;
run;
%macro Test(var_ls=&Var_lss);
proc sql;create table ttt2 as select * from ttt
where lowcase(x) not in (&Var_lss);quit;run;
%mend;
%Test(var_ls=month id); |
|
s******r 发帖数: 1524 | 10 data test;
input x $5.;
datalines;
ABC
BBC
DDD
DAD
;
run;
data test2;set test;
y=cats('27'x,x,'27'x);
z=quote(x);
run;
baozi, pls |
|
g**u 发帖数: 205 | 11 木有思路啊~~~就算给个提示也好 T_T
有这么个data set,学生体重
data weights;
input type age weight;
datalines;
t 6 77
t 7 84
s 6 88
s 6 68
s 7 92
x 6 53
x 7 69
x 7 88
x 8 84
;
run;
我想加个variable进去,叫Change,用来表示体重是增加了还是减少。如果前一个比后
一个高,type=1;如果前一个比后一个低,type=0。
想了好几个思路,if/then, sql,但都不知道怎么用code来比较。
麻烦了,帮我想想吧 |
|
s*********2 发帖数: 11 | 12 using dif1 function.
solution:
data weights;
input type $ age weight;
datalines;
t 6 77
t 7 84
s 6 88
s 6 68
s 7 92
x 6 53
x 7 69
x 7 88
x 8 84
;
run;
data change; set weights;
change=dif1(weight);
if change=. then type=.;
else if change<0 then type=1;
else if change>0 then type=0;
proc print data=change;run; |
|
k*******a 发帖数: 772 | 13 这样可能更简单些
data a;
input num product $;
datalines;
1 Lipitor
1 Crestor
1 Zetia
1 Zocor
;
data b;
set a(keep=product rename=(product=product1)) nobs=nobs;
do i=_N_+1 to nobs;
num+1;
product=product1;
output;
set a(keep=product) point=i;
output;
end;
drop product1;
run; |
|
k*******a 发帖数: 772 | 14 不知道这个方法对于上万个的时候效率如何
data a;
input id brand $;
datalines;
1 A
2 B
3 C
;
run;
proc sql;
create table b as
select a.id as id_i, a.brand as brand_i, b.id as id_j, b.brand as brand_j
from a as a, a as b
where a.id
quit; |
|
k*******a 发帖数: 772 | 15 data test;
input var1 $ var2 $;
datalines;
A no
A no
B yes
B no
B missing
C yes
C yes
C no
C missing
;
proc sql;
create table test1 as
select a.*, sum(var2="yes")>0 as flag
from test a
group by var1;
quit; |
|
k*******a 发帖数: 772 | 16 可以用 SAS 的dictionary来找出data有什么variable
data test;
input var1 $ var2 $;
datalines;
3.4 5
4.55 5.3
4 3.444
;
run;
proc sql noprint;
select strip(name)||"_n=input("||strip(name)||",best12.)" into :convert
separated by ";"
from sashelp.vcolumn
where libname="WORK" and upcase(memname)="TEST";
quit;
data test1;
set test;
&convert;
run; |
|
l*******a 发帖数: 107 | 17 我写了一个小macro, 程序是这样的:
%macro gg(name, length, price);
%put if &name.length=&length then &name.price=&price;
%mend;
data one;
input variableName $14. variableLength : $12. price;
datalines;
FundingAmount 0-15000 0.217
ObjectAmount 15000-30000 0.318
FundingAmount 30000-60000 0.519
;
run;
data two;
set one;
put 'this is the data';
put 'Please read this in details';
call execute('%gg('||variableName||', '||variableLength||', '||price||')');
run;
我想要的结果是:
this is the data
Please... 阅读全帖 |
|
f******u 发帖数: 250 | 18 %macro gg(name, length, price);
%put if &name.length=&length then &name.price=&price;
%mend;
data one;
input variableName $14. variableLength : $12. price;
datalines;
FundingAmount 0-15000 0.217
ObjectAmount 15000-30000 0.318
FundingAmount 30000-60000 0.519
;
run;
data two;
set one;
if _n_=1 then do;
put 'this is the data';
put 'Please read this in details';
end;
call execute('%gg('||variableName||', '||variableLength||', '||price||')');
run; |
|
l*******a 发帖数: 107 | 19 I want to save the log output into a sas file,
but in ths test.sas, the ouput is only like this:
this is the data
Please read this in details
How can I get all of the output in the saved sas file like this:
this is the data
Please read this in details
if FundingAmountlength=0-15000 then FundingAmountprice=0.217
if ObjectAmountlength=15000-30000 then ObjectAmountprice=0.318
if FundingAmountlength=30000-60000 then FundingAmountprice=0.519
Thanks!!!
%macro gg(name, length, price);
%put if &name.len... 阅读全帖 |
|
f******u 发帖数: 250 | 20 SAS uses clause "file print" to output put statement but I don't why this is
not working for Macro's %put.
data one;
input variableName $14. variableLength : $12. price;
datalines;
FundingAmount 0-15000 0.217
ObjectAmount 15000-30000 0.318
FundingAmount 30000-60000 0.519
;
run;
data two;
set one;
file print;
put 'this is the data';
put 'Please read this in details';
call execute('%gg('||variableName||', '||variableLength||', '||price||')');
run; |
|
k*******a 发帖数: 772 | 21 不知道是不是这个意思? 我新建了个变量 var_new, 不过可以把 var_new覆盖掉原来
的var 就可以了
data test;
input id var;
datalines;
1 0
1 0
1 1
2 0
2 0
3 1
3 0
;run;
proc sql;
create table test1 as
select a.*,
case when (sum(var=1)>0) then 1 else var end as var_new
from test a
group by id;
quit;
records |
|
m********l 发帖数: 791 | 22 多谢您的答复 你理解的没有错。
我不用proc sql做了一下, 也能够实现我想要的
data test;
input id var;
datalines;
1 0
1 0
1 1
2 0
2 0
3 1
3 0
;run;
proc sort data=test;
by id descending var;
run;
data test1;
set test;
by id;
if first.id = 1;
run;
data test2;
set test;
drop var;
run;
data test3;
merge test2 test1;
by id;
run;
PS:我的var变量是binary的,我用了proc sort和first.id以及merge来实现 |
|
|
k*******a 发帖数: 772 | 24 用两次transpose就可以了,只是变量顺序有点不同
data test;
input ID Attr1 $ Attr2 $;
datalines;
1 A1 B1
1 A2 B2
1 A3 B3
2 A4 B4
3 A5 B5
3 A6 B6
3 A7 B7
3 A8 B8
4 A9 B9
4 A10 B10
;
run;
proc transpose data=test prefix=attr1 out=test1(drop=_name_);
by ID;
var attr1;
run;
proc transpose data=test prefix=attr2 out=test2(drop=_name_);
by ID;
var attr2;
run;
data final;
merge test1 test2;
by ID;
run; |
|
y********0 发帖数: 638 | 25 data one;
input ID Attr1 $ Attr2 $;
datalines;
1 A1 B1
1 A2 B2
1 A3 B3
2 A4 B4
3 A5 B5
3 A6 B6
3 A7 B7
3 A8 B8
4 A9 B9
4 A10 B10
;
run;
data two;
set one;
v1=attr1; output;
v1=attr2; output;
run;
proc transpose data=two out=three(drop=_name_);
var v1;
by id;
run; |
|
y********0 发帖数: 638 | 26 data one;
input id$ A B C;
datalines;
p1 1 . .
p1 . 1 .
p2 . . .
p2 . . 1
p3 . . .
p3 1 . .
run;
proc sql;
create table two as
select id,max(A) as A,max(B) as B, max(C) as C
from one
group by id;
quit;
same |
|
k*******a 发帖数: 772 | 27 data a;
input ID year;
datalines;
1 1998
1 2001
2 1994
2 1995
2 1999
2 2001
3 1997
3 1999
3 2000
;
run;
proc sql;
create table minmax as
select distinct id, min(year) as min, max(year) as max
from a
group by id;
run;
data b;
set minmax;
do year = min to max;
output;
end;
drop min max;
run; |
|
l*******a 发帖数: 107 | 28 libname mylib 'C:\Research\Data';
data mylib.XYZ;
input x y z;
datalines;
1 1 12.4
1 2 11.3
1 3 1.4
2 1 2.1
2 2 19.4
2 3 10.0
;
run;
结果出现了这样的问题:
ERROR: User does not have appropriate authorization level for library MYLIB.
请教各位,应该怎么解决?
多谢! |
|
l*******a 发帖数: 107 | 29 不好意思 我测试的code 是:
libname mylib 'C:\Users\Desktop';
data mylib.XYZ;
input x y z;
datalines;
1 1 12.4
1 2 11.3
1 3 1.4
2 1 2.1
2 2 19.4
2 3 10.0
;
run;
为什么路径是desktop 就有错误呢?应该怎么解决呢?
多谢! |
|
l*******a 发帖数: 107 | 30 我贴code上来的时候做了些改动。 完整的是:
libname mylib 'C:\Users\lubinghua\Desktop';
data mylib.XYZ;
input x y z;
datalines;
1 1 12.4
1 2 11.3
1 3 1.4
2 1 2.1
2 2 19.4
2 3 10.0
;
run;
但是问题还是同样存在,大家run 这个code都没问题吗? |
|
h***x 发帖数: 586 | 31 没有问题
6 libname mylib 'C:\Users\Huxxx\Desktop';
NOTE: Libname MYLIB refers to the same physical library as HUXXX.
NOTE: Libref MYLIB was successfully assigned as follows:
Engine: V9
Physical Name: C:\Users\Huxxx\Desktop
7
8 data mylib.XYZ;
9 input x y z;
10 datalines;
NOTE: The data set MYLIB.XYZ has 6 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.00 seconds
17 ;
... 阅读全帖 |
|
|
k*******a 发帖数: 772 | 33 data a;
input date date9. akanr akber ;
format date date9.;
datalines;
31mar1999 1 2
30jun1999 2 3
30sep1999 4 5
31dec1999 1 2
;
run;
proc transpose data=a out=b;
by date;
var akanr akber;
run;
最后的结果排排序,该改名就可以了。 |
|
D******n 发帖数: 2836 | 34 Your script has a bug on all n/a situations. Following should resolve
it.
data raw;
input id (v1 - v5) ($);
datalines;
1 aa aa bb aa dd
2 er re er n/a er
3 n/a aa bb n/a aa
4 aa aa aa bb bb
5 aa aa aa aa aa
6 aa bb bb cc cc
7 n/a n/a n/a n/a n/a
7 aa n/a n/a n/a zz
;
run;
proc print;run;
data new;
set raw;
call sortc( of v1-v5);
array xs v:;
count=0;
do i=2 to dim(xs);
count+( xs(i-1) ne 'n/a' an... 阅读全帖 |
|
a****r 发帖数: 71 | 35 请教一个《Carpenter's Complete Guide to the SAS Macro》上SAS macro的例子。
Chapter 6, Page 111 有一个根据data A1 里两个变量 station 和 depth 生成 一系
列ACSII文件的macro的例子;
**********************;
***6.2.1d
***%DOIT
**********************;
* 1993 Water quality data.
*************************************************;
data a1
(keep=datetime station depth temp ph do cond salinity);
input datetime datetime13. @15 station $3.
depth temp ph do cond salinity;
label datetime = 'date and time of sample collection'
st... 阅读全帖 |
|
s********1 发帖数: 54 | 36 ______________________________________________
I got the following error message:
______________________________________________
ERROR: Insufficient authorization to access C:\Program Files\SASHome\
SASFoundation\9.3\TS30.dat
______________________________________________
My code is as follows:
______________________________________________
**********************;
***6.2.1d
***%DOIT
**********************;
* 1993 Water quality data.
*************************************************;
data a1
... 阅读全帖 |
|
k*******a 发帖数: 772 | 37 data test;
input id seq var;
datalines;
1 1 11
1 4 22
1 5 33
1 6 44
1 7 55
1 8 66
2 1 11
2 2 22
2 8 33
3 1 11
3 2 22
3 3 33
3 4 44
3 5 55
3 6 66
3 7 77
3 8 88
;
run;
data test2;
set test(rename=(var=var1));
by id;
var = lag(var1);
if first.id then var=.;
drop var1;
run; |
|
s******3 发帖数: 57 | 38 新手入门, 应该是道很简单的题目, 可就是没绕出来, 为什么value of macro
variable rc 是low而不是high哪?
data a;
input age;
datalines;
12
12
13
35
36
37
;
run;
%macro test;
data b;
set a nobs=totalobs;
if totalobs > 5 then do;
%let rc = high;
end;
else do;
%let rc = low;
end;
run;
%mend;
&test |
|
y*****w 发帖数: 1350 | 39 This could be a split-plot design. Treat DIET as the whole unit and TIME (
baseline, 6 mo, 12 mo) as the sub-units. Use the count (frequency) of each
diet within each id at each time point as the outcome variable. I’ve made
up a data as follows, assuming that at each time point an id could have any
combinations of diets but the total number of meals is always 4 (e.g. 2
meals of chicken, 0 meal of turkey, 2 meals of ham, 0 meal of fish for ID
#2 at baseline):
/*
coding of diet - chicken 1
... 阅读全帖 |
|
g******g 发帖数: 498 | 40 SAS初学者,想用SAS处理数据,但是对SAS了解的不是很多。麻烦高人指点。
Data test;
input A B C D;
datalines;
3.04 0.14 2.04 0.53
2.89 0.12 1.91 0.51
3.01 0.12 1.8 0.48
;
Proc print data=test; run;
data test2; set test;
A_B=A/B; A_C=A/C; A_D=A/D; B_C=B/C; B_D=B/D; C_D=C/D;
PROC PRINT; RUN;
问题
如果A_B>21, 我想得到这样的结果: A_B2=((A_B/2.5)-1)*1000/50;
如果A_B<=21,我想得到这样的结果: A_B2=(1-(2.5/A_B))*1000/50;
同理,其他的变量(比如 A_C, A_D等等)也想得到类似的关系。不知道是不是要用到
if 语句,我试着用,但是不work,估计是我哪里出问题了。
可能问题比较幼稚,先谢谢了。 |
|
k******u 发帖数: 250 | 41 创建一个新的variable z,用如下code
data bank;
infile 'C:bankdata.txt' firstobs =2;
input Name $ 1-15
Acct $ 16-20
x 21-26
y 27-30;
z = x * y;
run;
proc print data = bank;
run;
这个程序works good,
但是当我把infile 去掉,用datalines;的statement输入x y的值,同时在data中计算
z=x*y,却被告知statement is not valid。
为什么呢? |
|
M*N 发帖数: 435 | 42 it is working ya
data bank;
input x y ;
z = x * y;
datalines;
1 3
3 4
4 5
;
run; |
|
k******u 发帖数: 250 | 43 thx.
我按照你的把z=x*y写在datalines之前就work了,但是写在之后就是不work。
thx |
|
l****V 发帖数: 10 | 44 %Macro loop;
%do i=1 %to 12;
%let varlist_m&i =%str(
dlq30_m&i. ,
Active_m&i.
);
data test&i;
input &&varlist_m&i;
datalines;
10 20
;
run;
%end;
%mend loop;
%loop |
|
k*******a 发帖数: 772 | 45 我怀疑是这一步 %let varlist_m&i =%str(
dlq30_m&i. ,
Active_m&i.
);
当你调用 &&varlist_m&i 时候, 第一次解析 &i, 变成比如 &varlist_m1
但是因为有 %str, 所以他编程 dlq30_m&i. , Active_m&i.
这里 &i 还是没有解析
所以我建议把 %str去掉,或者直接
%Macro loop;
%do i=1 %to 12;
data test&i;
input dlq30_m&i. , Active_m&i.;
datalines;
10 20
;
run;
%end;
%mend loop;
%loop |
|
g**u 发帖数: 205 | 46 比如说你design了一个study,是10天的,如果能活过10天的,就是censor的,一般是
用1来表示,censor(1)。在SAS中,你可以自己输入一个column来专门indicate
censor,0 或者 1。
data a;
input ID time censor;
datalines;
1 5 0
2 6 0
3 10 1
4 8 0
5 4 0
;
run;
大概意思就是这样的. |
|
c**********e 发帖数: 2007 | 47 Could you anybody test running the following SAS code? Of course, the output
path should be changed to yours. Thanks a ton!
data one;
input x y;
datalines;
-1 -1
-1 0
0 -1
0 1
1 0
1 1
;
run;
proc export DBMS=CSV
data=one
outfile="C:/Users/CareerChange/My Documents/one.csv"
replace;
putnames=yes;
run;
quit; |
|
k*******a 发帖数: 772 | 48 A stupid way:
data test;
input Ids $ Status Date mmddyy10.;
datalines;
ID1 4 05/02/2013
ID1 3 05/10/2013
ID1 2 05/16/2013
ID1 1 05/20/2013
ID2 3 05/08/2013
ID2 2 05/10/2013
ID2 1 05/19/2013
;
run;
proc sql;
select distinct Ids,
(select date from test where Ids = a.Ids and status=3) - (
select date from test where Ids = a.Ids and status=4) as chg43 label = "From
4-3 (Days)",
(select date from test where Ids = a.Id... 阅读全帖 |
|
j******o 发帖数: 127 | 49 try this:
data have;
input id $ A $ B $;
datalines;
a x 1
. x 2
b y 3
. y 4
. y 5
c x 6
. y 7
;
run;
data obtain;
set have;
retain ID_new;
if id ^='' then ID_new=ID;
run; |
|
p***7 发帖数: 535 | 50 It should be simple for experienced programmers. The difficulty is the
special symbol in the pet_owner variable and its different length. In infile
command we could use $varying, but when it comes with cards/datalines, it
is clueless.
Please help!
Pet_owner pet number
Mr. Black dog 2
Mr. Black bird 1
Mrs. Green fish 5
Mr. White cat 3 |
|