o****o 发帖数: 8077 | 1 if all ids are capitalized Char A-to-Z, then it is easy:
*-----------------------;
data yourdata;
input group_id id1 $ ID $;
datalines;
1 A B
1 A C
1 A D
2 B A
2 B C
2 B D
3 C B
4 D A
4 D B
5 B E
5 B K
5 B E
6 B F
7 E K
;
run;
data newdata;
array _C1[65:90] _temporary_;
do i=65 to 90; _C1[i]=0; end;
do until (eof);
|
|
a********a 发帖数: 346 | 2 How can I transfer the data structure from one to two? Basically I want to
get the visit date difference for other visits to visit 0, and value
difference for other visits to visit 0. Thanks
data one;
input id visit date ddmmyy10. value;
format date ddmmyy10.;
datalines;
1 0 1/1/2010 0.5
2 0 3/1/2010 0.8
2 2 5/1/2010 1.5
2 4 7/1/2020 0.3
3 0 4/5/2010 1
3 2 6/5/2010 0.9
;
r |
|
D******n 发帖数: 2836 | 3 data a1;
retain a 1;
do b=1 to 3;
input c @@;
output;
end;
a=a+1;
datalines;
1 2 3
4 5 6
1 4 5
;
run; |
|
l******1 发帖数: 86 | 4 想用SAS解一个三次的方程,但是希望根的取值在0和1。这可怎么弄啊。
code如下。
data data1;
input p_ta;
datalines;
0.975
;
proc model data=DATA1;
eq.a=(1-ta)**3+3*ta*(1-ta)**5-p_ta;
solve ta /onepass solveprint;
run; |
|
g**a 发帖数: 2129 | 5 data one;
input sche date10.;
format sche mmddyy10.;
datalines;
4-Dec-08
18-Dec-04
13-Sep-07
15-Sep-07
8-May-07
;
run;
proc print data=one;
run;
that code works for me. |
|
g**a 发帖数: 2129 | 6 data one;
input name $ type $;
datalines;
A 1
A 1
A 2
B 1
C 1
C 3
C 2
;
run;
proc sort data=one;
by name;
run;
data Two;
length alltype $500;
set one;by name;
name=name;
do until (last.name);
if first.name then alltype=type;
else alltype=catt(alltype,type);
set one;by name;
end;
if first.name then alltype=type;
else alltype=cats(alltype,type);
output;
run;
proc sql;
create table three as
SELECT one.name, one.type, two.alltype FROM ONE INNER JOIN TWO ON one.name=tw |
|
h**********e 发帖数: 44 | 7 First create a dataset:
data index;
input i;
datalines;
1
3
6
7
run;
Then
%do j=1 &to 4;
data a;
set index (obs=&j);
call symput('i',i);
run;
/*do whatever you want to do with &i here*/
...
%end;
This is what I usually do this kind of work. Remember macro in SAS is not as
easy as function in C. SAS is dataset oriented in most of the cases. |
|
g**a 发帖数: 2129 | 8 data one;
input FirmID $ Year earnings $ Mean Variance ;
datalines;
BBB 2004 E1 100 8
BBB 2005 E2 300 10
BBB 2006 E3 600 12
BBB 2007 E4 530 30
BBB 2008 E5 410 28
;
run;
proc IML;
use one;
read all;
xmean=mean;
xvar=variance;
xyear=year;
Numrow=Nrow(xmean);
do y=1 to Numrow;
sim=rand('NORMAL', mean[y], sqrt(variance[y]));
do x=2 to 250 by 1;
sim=sim||rand('NORMAL', mean[y], sqrt(variance[y])); |
|
D******n 发帖数: 2836 | 9 data a1;
input var1 $;
datalines;
A
A
B
B
B
C
C
C
D
D
;
data a1;set a1;by var1;
retain match_key 0;match_key=match_key+1;
if first.var1 then match_key=1;run;
data a2;set a1;by var1;
if first.var1 then do;
do var2=1 to 3;
rand=ranuni(100);
output;
end;
end;drop match_key;run;
proc sort data=a2;by var1 rand;run;
data a2;set a2;by var1;
retain match_key 0;match_key=match_key+1;
if first.var1 then match_key=1;run;
proc sql;select a1.var1,a2.var2 from a1,a2
where a1.var1=a2.va |
|
b******e 发帖数: 539 | 10 还是不对,试试这组简单的数据:
data a3;
input id1 id2;
datalines;
3 2
5 2
4 4
5 4
5 5
;
run;
data a2;
set a3;
idnew=id1;
run;
%setID(id2); |
|
g**a 发帖数: 2129 | 11 data one;
input a $ b c mmddyy10.;
datalines;
s1 10 02/16/2010
s1 20 02/10/2010
s1 30 01/16/2010
s1 40 04/16/2010
s1 50 02/21/2010
s1 60 02/28/2010
s1 70 03/16/2010
s2 110 02/16/2010
s2 120 02/10/2010
s2 130 01/16/2010
s2 140 04/16/2010
s2 150 02/21/2010
s2 160 02/12/2010
s2 170 03/16/2010
s2 180 02/06/2010
s2 190 02/08/2010
s3 210 02/16/2010
s3 220 02/10/2010
s3 230 01/16/2010
s3 240 04/16/2010
s3 250 02/21/2010
s3 260 02/19/2010
;
run;
proc sort data=one;
by a c;
run;
proc print data=one;
run; |
|
z****n 发帖数: 67 | 12 现在有一个data set,如下:
data survey;
input id diet exer hours xwk educ;
datalines;
1 1 . 1 3 1
1 . 2 1 4 2
1 . 4 . . .
1 1 5 2 3 .
2 . 9 2 3 .
2 5 9 2 4 .
2 . 3 . 5 3
3 2 . . . .
;
我有一个macro可以自动count给定列的每个id的missing value个数,但只能够给出一
列。如果
我想要写一个macro,运行一次自动给出多列,比如此列中diet exer educ的missing
value
该怎么改呢,是否该用array?(实际我要用的data set 有300 多个变量需要测missing
value)
options nodate pageno=1 linesize=80 pagesize=60;
%macro countm(col);
count(case
when &col~= . then "count me"
end) as N |
|
o****o 发帖数: 8077 | 13 afternoon time...
data original;
input x1 $ x2 $ x3;
datalines;
a b 1
b a 3
a c 2
;
run;
data newview/view=newview;
set original;
array _c{*} x1 - x2;
call sortc(of x1-x2);
_newid=cats(of x1-x2);
run;
proc sort data=newview out=newdata;
by _newid x3;
run;
/* keep max*/
data newdata1;
set newdata; by _newid;
if last._newid;
drop _newid;
run;
/* keep first one*/
data newdata2;
set newdata; by _newid;
if first._new |
|
s******r 发帖数: 1524 | 14 data one;
input ID a1 a2 a3 a4 a5;
datalines;
1 0 0 0 0 0
2 0 0 0 0 0
3 0 1 1 0 0
;
run;
data test(keep=id) two;
set one;
if max(of a1-a5)=0 and min(of a1-a5)=0 then output test;
else output two;run;
data two;set test two ;run; |
|
y****n 发帖数: 46 | 15 data one;
array aa(5) a1-a5;
input ID a1 a2 a3 a4 a5 abc cnn nb;
do i=1 to 5;
if aa(i)=0 then aa(i)=.;
end;
drop i;
datalines;
1 0 0 0 0 0 5 6 7
2 0 0 0 0 0 7 4 9
3 0 1 1 0 0 4 4 4
;
run; |
|
y****n 发帖数: 46 | 16 data one;
array aa(5) a1-a5;
input ID a1 a2 a3 a4 a5 abc cnn nb;
if sum(of a1-a5)=0 then do;
do i=1 to 5;
if aa(i)=0 then aa(i)=.;
end;
end;
drop i;
datalines;
1 0 0 0 0 0 5 6 7
2 0 0 0 0 0 7 4 9
3 0 1 1 0 0 4 4 4
;
run; |
|
l**********s 发帖数: 255 | 17 Thanks...Just let you know that the results will be different with what I
want.
The following is what I get after running this code.
data two;
input ID a1 a2 a3 a4 a5 abc cnn nb;
datalines;
1 . . . . . 5 6 7
2 . . . . . 7 4 9
3 . 1 1 . . 4 4 4 |
|
s******r 发帖数: 1524 | 18 also the following code could fail if negative number is possible from a1 to 15.
Thanks...Just let you know that the results will be different with what I
want.
The following is what I get after running this code.
data two;
input ID a1 a2 a3 a4 a5 abc cnn nb;
datalines;
1 . . . . . 5 6 7
2 . . . . . 7 4 9
3 . 1 1 . . 4 4 4 |
|
s*********e 发帖数: 1051 | 19 data one;
input firm $1. year;
datalines;
A 1990
A 1991
A 1993
b 1978
b 1979
b 1980
c 2001
c 2009
d 2004
d 2006
e 1999
f 1990
f 1997
f 1999
;
run;
proc sql;
create table
two as
select
*
from
one
where
firm in
(select
distinct a.firm
from
one as a, (select * from one group by firm having year > min(year)) as b
where
a.firm = b.firm and a.year + 1 = b.year
);
quit; |
|
k*******a 发帖数: 772 | 20 写了一个,不过有点繁
data a;
input id $ week value1;
datalines;
a 1 12
a 2 22
a 3 33
a 4 .
a 5 .
a 6 .
a 7 .
b 1 78
b 2 .
b 3 .
b 4 .
b 5 .
b 6 .
b 7 .
b 8 .
c 1 88
c 2 67
c 3 76
c 4 .
c 5 .
c 6 .
;
data b;
set a;
by id;
if value1 ne . then do
value2=value1;
retain value2;
end;
if last.id then output;
keep id value2;
data a;
merge a b;
by id;
proc print data=a;run; |
|
k*******a 发帖数: 772 | 21 这个应该不是版本的问题。
如果你用的是datalines, 那么每行你的数据后面都加上很多空格(你看不到),但是
程序读你数据的时候,把空格读进去了,也就不会miss掉。
如果你把数据放到文件里面,那么数据后面不会有空格,而是endofline,所以
missover的时候,读pilot因为独到endofline所以就以为数据不全,读为miss
missover |
|
a*****3 发帖数: 601 | 22 data test2 ;
input @1 q1 date9. ;
format q1 yymmdd10. ;
datalines;
12NOV1993
;
run;
还有个问题,没看完你就删了? |
|
k*******a 发帖数: 772 | 23 sql version:
data a;
input x;
datalines;
1
2
2
3
;run;
proc sql;
select a.*
from a as a, a as b
where a.x=b.x
group by a.x
having count(*)=1
; |
|
s*********y 发帖数: 34 | 24 Q #27
data temp;
input day month$ temp;
datalines;
1 may 75
15 may 70
15 june 80
3 june 76
2 july 85
14 july 89
;
run;
proc sort data=temp;
by descending month day;
run;
proc print data=temp;
run;
Why the result is :
Obs day month temp
1 1 may 75
2 15 may 70
3 3 june 76
4... 阅读全帖 |
|
k*******a 发帖数: 772 | 25 这个行吗
data a;
input nameid year indicator;
datalines;
1 1988 1
1 1989 1
1 1990 0
2 1985 0
2 1986 1
2 1987 0
2 1988 1
2 1989 0
2 1990 1
2 1991 1
;
data a;
set a;
by nameid year;
if first.nameid then sum=0;
sum+indicator;
proc print;run; |
|
s******r 发帖数: 1524 | 26 都抢上了,只好不客气了。
data aaa;
input nameid year indicator;
datalines;
1 1988 1
1 1989 1
1 1990 0
2 1985 0
2 1986 1
2 1987 0
2 1988 1
2 1989 0
2 1990 1
2 1991 1
;
run;
proc sql;
select a.nameid, a.year, sum(b.indicator)
from aaa a inner join aaa b
on a.nameid=b.nameid and a.year>=b.year
group by a.nameid,a.year;quit;run; |
|
o****o 发帖数: 8077 | 27 data test;
input NameID Year Indicator ;
datalines;
1 1988 1
1 1989 1
1 1990 0
2 1985 0
2 1986 1
2 1987 0
2 1988 1
2 1989 0
2 1990 1
2 1991 1
;
run;
proc expand data=test out=test;
by NameID;
convert indicator=is/transformout=(cusum);
run;
baozi pls. 3ks |
|
y**********0 发帖数: 425 | 28 DATA TEST;
INPUT X Y Z;
DATALINES;
1 3 15
7 13 7
8 12 5
3 4 14
4 7 10
;
PROC REG DATA=TEST;
MODEL Y=X;
PLOT PREDICTED.*X='P' Y*X='O'/OVERLAY;
RUN;
There is no regression line,not like what the book said.
And there is no sign of P or O,
how to solve this problem.
Thank you. |
|
s*********e 发帖数: 944 | 29 data flowers;
input type $ 1-5 +1 color $;
datalines;
daisyyellow
;
run;
proc print;
run;
* Obs type color
1 daisy ellow;
我不明白在input那一行,+1是什么意思?
谢谢! |
|
k*******a 发帖数: 772 | 30 data a;
array col{5} $;
do i=1 to 5;
input col{i} $ @;
if input(col{i},8.) ne . then col{i}=0;
end;
datalines;
a34 33 34 bbd 56.5
bbb 33 2 ddf 12
;
proc print data=a;run;
proc transpose data=a out=b;
var col1-col5;run;
data b;
set b;
if col1 ne 0;
proc print data=b;run;
proc transpose data=b out=c(drop=_name_);
var col1-col2;
run;
proc print data=c;run; |
|
k*******a 发帖数: 772 | 31 data a;
input no sub_no;
datalines;
10004 1
10004 2
10005 1
10006 1
10006 1
10006 2
10006 3
10007 1
;
data a;
set a;
by no;
if first.no and last.no then delete;
proc print data=a;run; |
|
j**********3 发帖数: 305 | 32
用retain 跟 first. 做个counter,不就好了
data a;
input no sub_no;
datalines;
10004 1
10004 2
10005 1
10006 1
10006 1
10006 2
10006 3
10007 1
;
data a;
set a;
by no;
retain counter;
if first.no then counter=0;
counter+1
run;
data sub_no2;
set a;
if counter=2 then output ;
run; |
|
s*********e 发帖数: 1051 | 33 a sql solution just for fun.
data one;
i + 1;
input id $ eventDay;
datalines;
001 3
002 2
003 4
003 5
004 3
004 4
004 5
004 11
004 20
;
run;
proc sql;
create table
two (drop = i) as
select
a.*,
max(0, b.eventday) as start,
a.eventday as stop
from
one as a left join one as b
on a.id = b.id and a.i - 1 = b.i;
quit; |
|
j******o 发帖数: 127 | 34 这个方法是不是特别笨? 欢迎大家测试一下。假设事先知道给定字符的长度n。
%let n=20;
data have;
input str : $&n..;
datalines;
abcsdabcedbcsdaedfjs
;
run;
data have1;
set have;
do i=1 to &n;
do j=1 to &n-i;
sub=substr(str, i, j);
len=lengthn(sub);
output;
end;
output;
end;
run;
proc sort data=have1 nodupkey;
by i sub;
run;
proc sort data=have1;
by len sub;
run;
data have1;
set have1;
sub1=lag(sub);
if sub not eq sub1 then delete;
run;
data have1 (keep=str len);
set have1 end=las... 阅读全帖 |
|
m*****a 发帖数: 658 | 35 也考虑了多个patient 的情况:
data try;
input patient LD;
datalines;
1001 5
1001 45
1001 70
1002 10
1002 20
1002 35
run;
proc sort data=try;
by patient descending LD;
run;
data try2;
set try;
change=lag(LD)-LD ;
if lag(patient)~=patient then change=".";
run;
proc sort data=try2;
by patient LD;
run; |
|
m*****a 发帖数: 658 | 36 Thanks, PharmD. It is very useful.
data try;
input patient LD;
datalines;
1001 5
1001 45
1001 70
1002 10
1002 35
1002 20
run;
proc expand data=try out=try1 method=none;
convert LD=LD_lead /transformout=(lead 1);
by patient;
run;
data try2 ;
set try1;
change=LD_lead-LD;
keep patient LD change;
run; |
|
a********i 发帖数: 205 | 37 多谢各位大神
总结一下还是这样写最好:
data old;
input patient LD;
datalines;
1001 5
1001 45
1001 70
1002 10
1002 20
1002 35
run;
proc sort data=old;
by patient;
run;
Data new(drop=LD2);
merge old old(firstobs=2 rename=(LD=LD2));
by patient;
change=LD2-LD;
if last.patient then change=.;
run; |
|
k*******a 发帖数: 772 | 38 我这个似乎work,不过有点复杂,还是用proc expand最好
data try;
input patient LD;
datalines;
1001 5
1001 45
1001 70
1002 10
1002 35
1002 20
run;
data change;
set try;
by patient;
change=LD-lag(LD);
output;
if last.patient then do;
change=.;
output;
end;
data change;
set change;
by patient;
if first.patient ne 1;
keep change;
data try;
merge try change;
proc print data=try;run; |
|
k*******a 发帖数: 772 | 39 你可以新建一个变量,把他们group起来
data a;
input year id $ sequence x;
datalines;
2000 id1 1 1
2001 id1 2 2
2002 id1 3 2
2005 id1 1 2
2006 id1 2 3
2001 id2 1 6
2002 id2 2 7
2004 id2 1 1
2007 id2 1 2
2009 id2 1 8
2000 id3 1 9
2001 id3 2 1
2002 id3 3 2
2003 id3 4 3
2004 id3 5 4
2008 id3 1 6
;
data a;
set a... 阅读全帖 |
|
a********i 发帖数: 205 | 40 data sum;
input year id $ sequence x;
datalines;
2000 id1 1 35
2001 id1 2 60
2002 id1 3 80
2005 id1 1 76
2006 id1 2 95
2001 id2 1 108
2002 id2 2 87
2004 id2 1 76
2007 id2 1 84
2009 id2 1 98
2000 id3 1 123
2001 id3 2 198
2002 id3 3 82
2003 id3 4 90
2004 id3 5 23
2008 id3 1 90
;
run;
... 阅读全帖 |
|
o****o 发帖数: 8077 | 41 ZT from :
http://www.globalstatements.com/sas/jobs/technicalinterview.htm
*****************************************
SAS Technical Interview Questions
You can go into a SAS interview with more confidence if you know that you
are prepared to respond to the kind of technical questions that an
interviewer might ask you. I do not provide the specific answers here, both
because these questions can be asked in a variety of ways and because it is
not my objective to help those who have little actual int... 阅读全帖 |
|
a********s 发帖数: 188 | 42 在网上看到一个类似的。
data a;
input a$;
datalines;
00121
0121
0122
122
AAAA
BBb
;
run;
data b(drop = x);
set a;
do until (x ne "0");
x=first(a);
if x="0" then substr(a,1,1)=" ";
a = left(a);
end;
run;
run; |
|
d*******r 发帖数: 71 | 43 Just make another variable eq to id, please look
data test1;
input id x y;
datalines;
1 1 5
1 1 2
1 1 3
1 2 8
1 2 7
1 2 10
1 2 4
;
run;
data test1;
set test1;
xc=x;
run;
data proc sort data=test1;
by id x xc;
run;
proc report data=test1 out=test2;
column x id xc y;
define x/order noprint;
define id/ order;
define xc/order;
define y/ display;
run; |
|
k*******a 发帖数: 772 | 44 试一下,肯定有更好的办法
data a;
input company $ 1-2 city $ profit comma18.;
datalines;
A BJ 15,000
SH 56,000
B GZ 34,555
HK 43,222
;
data a(drop=lagcompany);
set a;
lagcompany=lag(company);
if company=' ' then company=lagcompany;
proc print data=a; run; |
|
k*******a 发帖数: 772 | 45 it worked for me with lag just now, but not sure why it is not working, so i
tried retain...
data a;
input company $ 1-2 city $ profit comma18.;
datalines;
A BJ 15,000
JJ 12,335
jr 12,345
BB 12,445
B GZ 34,555
HK 43,222
;
data a(drop=rcompany);
set a;
if company=' ' then company=rcompany;
rcompany=company;
retain rcompany;
proc print data=a; run; |
|
k*******a 发帖数: 772 | 46 对10取余数就可以啦
如果只要去最后一位7,8,9 那么很简单
如果要去最后连续几位7,8,9那可以用do while循环
data a;
input x;
modx=mod(x,10);
if modx in (7,8,9) then x=(x-modx)/10;
datalines;
19
18
129
139
1329
28
229
2337
39
328
3239
;
proc print data=a;run; |
|
k*******a 发帖数: 772 | 47 不知道这个可不可以,你这里只有一个patient啊
data a;
input id visit MMDDYY10. status;
format visit MMDDYY10.;
datalines;
1 1/20/2011 1
1 1/23/2011 1
1 1/25/2011 2
1 1/28/2011 1
1 1/30/2011 1
1 2/13/2011 2
1 2/16/2011 2
1 2/20/2011 2
1 2/25/2011 1
;
proc print data=a;run;
data b(drop=out2);
set a;
by id;
retain out2;
if first.id then out2=0;
if out2=0 and status=2 then do;
out2=1;
output;
end;
else if out2=1 and status=1 then do;
out2=0;
output;
end;
proc print ... 阅读全帖 |
|
t*****s 发帖数: 49 | 48 路过的牛人请分析一下。我下周考base。多谢先。
Q30 我的疑问---关于auto conversion of character-to-numeric variable:
当character variable和一个numeric value比较时,auto conversion will happen,
为什么这里不行?
when a character variable compared to a numeric value, the character
variable will be auto-converted to numberic variable --- from SAS base Prep
Guide P402
以下程序我在sas上试过了,的确是d,但不明白为什么这里不自动转换。
Q30.The descriptor and data portions of the Work.Salaries data set are shown
below.
Variable Type Len Posname Char 8 0
salary status ... 阅读全帖 |
|
k*******a 发帖数: 772 | 49 比较笨的方法。。。
data a;
input A 4. B 4. C 4. D 4. ;
datalines;
1
7
3 3
2 5
5
5 3
4 5
5 8
2 3
1
;
proc print;run;
data a;
set a;
if A ne . then ranka+1;
if B ne . then rankb+1;
if C ne . then rankc+1;
if D ne . then rankd+1;
run;
proc sql;
create table new as
select A, B, C, D
from
(select A, ranka from a where A ne . ),
(select B, rankb from a where B ne .) ,
(select C, rankc from a where C ne . ),
(select ... 阅读全帖 |
|
R*********i 发帖数: 7643 | 50 data test;
input A 4. B 4. C 4. D 4. ;
datalines;
1
7
3 3
2 5
5
5 3
4 5
5 8
2 3
1
;
run;
data test1;
merge test(keep=a where=(a>.))
test(keep=b where=(b>.))
test(keep=c where=(c>.))
test(keep=d where=(d>.));
run;
However there would be a warning message. |
|