p*****o 发帖数: 543 | 1 如下的数据:
NAME TYPE
A 1
A 1
A 2
B 1
C 1
C 3
C 2
请问我如何可以得到这个DATA SET呢?
NAME TYPE TYPEALL
A 1 112
A 1 112
A 2 112
B 1 1
C 1 132
C 3 132
C 2 132
就是想要多一个变量,这个变量是把同样NAME的人的TYPE都连接到一起。
觉得应该用RETAIN, FIRST.NAME来做,可是能力有限,做不出来。。。
请教大家 |
l*********s 发帖数: 5409 | 2 some thing like this:
data temp;
length typeall 200;
retain typeall;
set a; by name;
if first.name then typeall=type;
else typeall=cat(typeall,type);
run; |
p*****o 发帖数: 543 | 3 ok. maybe i should try cat function. i used typeall = typeall||type and it
doesnt work.
thanks |
p*****o 发帖数: 543 | 4 I THINK IT DOESNT WORK EITHER.... |
T*******I 发帖数: 5138 | 5 Your code won't work.
Here is a stupid but working code:
data a;
input Name $ Type;
cards;
A 1
A 1
A 2
B 1
C 1
C 3
C 2
;
run;
data comb;
set a;
if name='A' and type in (1,2) then typeall=112;
if name='B' and Type=1 then typeall=1;
if name='C' and type in (1,2,3) then typeall=132;
run;
proc print data=comb; run;
【在 l*********s 的大作中提到】 : some thing like this: : data temp; : length typeall 200; : retain typeall; : set a; by name; : if first.name then typeall=type; : else typeall=cat(typeall,type); : run;
|
p*****o 发帖数: 543 | 6 thank you but i have thousands of records....not just A,B,C, and more than
just 1 2 3 as Type....
【在 T*******I 的大作中提到】 : Your code won't work. : Here is a stupid but working code: : data a; : input Name $ Type; : cards; : A 1 : A 1 : A 2 : B 1 : C 1
|
T*******I 发帖数: 5138 | 7 You need to do a
proc freq
first, then list all of them in your code.
or Asking SAS support center with your side code of the SAS version. They
could help you.
If there is no shortcut or a funciton in SAS to do so, you have to do in
that way I suggest to you.
【在 p*****o 的大作中提到】 : thank you but i have thousands of records....not just A,B,C, and more than : just 1 2 3 as Type....
|
A*******s 发帖数: 3942 | 8 我觉得应该是proc transpose再和原数据merge
【在 p*****o 的大作中提到】 : 如下的数据: : NAME TYPE : A 1 : A 1 : A 2 : B 1 : C 1 : C 3 : C 2 : 请问我如何可以得到这个DATA SET呢?
|
l***a 发帖数: 12410 | 9 +1
but op didn't explain if type can have duplicate. if it can, then transpose
will have problem as well
【在 A*******s 的大作中提到】 : 我觉得应该是proc transpose再和原数据merge
|
p*****o 发帖数: 543 | 10 at least get some ideas. thank you guys!
originally, i thought it would be very simple and easy... |
|
|
s******y 发帖数: 352 | 11 does it do what you want?
proc sort data=have;
by name;
run;
data want;
if 0 then set have;
length typeall $50.;
do _n_=1 by 1 until(last.name);
set have;
by name;
typeall=cats(typeall,type);
end;
do _n_=1 to _n_;
set have;
output;
end;
run;
【在 p*****o 的大作中提到】 : 如下的数据: : NAME TYPE : A 1 : A 1 : A 2 : B 1 : C 1 : C 3 : C 2 : 请问我如何可以得到这个DATA SET呢?
|
l*********s 发帖数: 5409 | 12 My original code has a typo error, plz replace the second "cat" function
with "cats"
【在 p*****o 的大作中提到】 : ok. maybe i should try cat function. i used typeall = typeall||type and it : doesnt work. : thanks
|
T*******I 发帖数: 5138 | 13 Your question is about "combining values" of a variable. I believe that the
current SAS does not provide such a function.
However, I believe that SAS will give a valuable instruction.
【在 T*******I 的大作中提到】 : You need to do a : proc freq : first, then list all of them in your code. : or Asking SAS support center with your side code of the SAS version. They : could help you. : If there is no shortcut or a funciton in SAS to do so, you have to do in : that way I suggest to you.
|
b*******r 发帖数: 152 | 14 it shall be pretty straightforward - just do a proc freq; by name; table
type/out=x...; then do sth on x. |
f*******e 发帖数: 51 | 15 mine
应该有更简单的
data test;
input name $ type;
cards;
A 1
A 1
A 2
B 1
C 1
C 3
C 2
;
run;
data temp;
length typeall $200;
retain typeall;
set test;
by name notsorted;
if first.name then typeall=strip(type);
else typeall=strip(typeall)||strip(type);
if last.name then output;
run;
proc sql noprint;
create table new as
select a.*, b.typeall
from test as a, temp as b
where a.name=b.name;
quit;
【在 p*****o 的大作中提到】 : 如下的数据: : NAME TYPE : A 1 : A 1 : A 2 : B 1 : C 1 : C 3 : C 2 : 请问我如何可以得到这个DATA SET呢?
|
l*********s 发帖数: 5409 | 16 Acutaries brother, do you know any sas function to add quotation marks for
each word in text? Many thanks.
【在 A*******s 的大作中提到】 : 我觉得应该是proc transpose再和原数据merge
|
s******y 发帖数: 352 | 17 not your brother. but you can try [quote] function. |
l*********s 发帖数: 5409 | 18 Hehe, you are , you all are. :-)
【在 s******y 的大作中提到】 : not your brother. but you can try [quote] function.
|
s******y 发帖数: 352 | 19 that is so called brotherhood, right.
well, I thin you asked how to add quotation mark to each word in the
sentence. the QUOTE function merely add one at begining and one at the very
end.
so for this task, I would like to use Regex. it is one liner solution. but
certainly, you can loop through and add the quotation mark for each word
delimited by space.
any way here is the code:
data _null_;
infile cards truncover;
length quoted_line $600.;
input line $200.;
quoted_line=prxchange('s/([\w''""]+)(
【在 l*********s 的大作中提到】 : Hehe, you are , you all are. :-)
|
l*********s 发帖数: 5409 | 20 Bull man, another bull man //cong
very
【在 s******y 的大作中提到】 : that is so called brotherhood, right. : well, I thin you asked how to add quotation mark to each word in the : sentence. the QUOTE function merely add one at begining and one at the very : end. : so for this task, I would like to use Regex. it is one liner solution. but : certainly, you can loop through and add the quotation mark for each word : delimited by space. : any way here is the code: : data _null_; : infile cards truncover;
|
|
|
l*********s 发帖数: 5409 | 21 Orz, your head figure ....
【在 s******y 的大作中提到】 : that is so called brotherhood, right. : well, I thin you asked how to add quotation mark to each word in the : sentence. the QUOTE function merely add one at begining and one at the very : end. : so for this task, I would like to use Regex. it is one liner solution. but : certainly, you can loop through and add the quotation mark for each word : delimited by space. : any way here is the code: : data _null_; : infile cards truncover;
|
s******y 发帖数: 352 | 22 see. that proves you think too much! haha.. |
l*********s 发帖数: 5409 | 23 hei...hei...
【在 s******y 的大作中提到】 : see. that proves you think too much! haha..
|
A*******s 发帖数: 3942 | 24 your sas skill is great!!
I totally couldnt understand this
quoted_line=prxchange('s/([\w''""]+)(?=\b)/"$1"/io',-1,line);
very
【在 s******y 的大作中提到】 : that is so called brotherhood, right. : well, I thin you asked how to add quotation mark to each word in the : sentence. the QUOTE function merely add one at begining and one at the very : end. : so for this task, I would like to use Regex. it is one liner solution. but : certainly, you can loop through and add the quotation mark for each word : delimited by space. : any way here is the code: : data _null_; : infile cards truncover;
|
A*******s 发帖数: 3942 | 25 besides smileguy's way, i think you can also use tranwrd function to do that.
say,
quoted_line=cats('"', tranwrd(trim(line), ' ', '" "'), '"');
【在 l*********s 的大作中提到】 : Acutaries brother, do you know any sas function to add quotation marks for : each word in text? Many thanks.
|
s*******f 发帖数: 148 | 26 it's perl.
【在 A*******s 的大作中提到】 : your sas skill is great!! : I totally couldnt understand this : quoted_line=prxchange('s/([\w''""]+)(?=\b)/"$1"/io',-1,line); : : very
|
g**a 发帖数: 2129 | 27 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 |
s******o 发帖数: 105 | 28 你这个根本不能叫解决问题的方法把。太naive了。
【在 T*******I 的大作中提到】 : Your code won't work. : Here is a stupid but working code: : data a; : input Name $ Type; : cards; : A 1 : A 1 : A 2 : B 1 : C 1
|
s******o 发帖数: 105 | 29 这个code应该是可以work的。稍微改改。typeall 应该定义成string
再看看你原来type是什么类型。
【在 l*********s 的大作中提到】 : some thing like this: : data temp; : length typeall 200; : retain typeall; : set a; by name; : if first.name then typeall=type; : else typeall=cat(typeall,type); : run;
|
i****w 发帖数: 329 | 30 name type
D 6
D 14
你希望怎么处理? |
|
|
S******y 发帖数: 1123 | 31 #A simple Python solution
in_file = 'D:\\my_data.txt'
f = open(in_file, 'r')
ls = []
tmp_name = ''
f.next() #skip header
for line in f:
name, typex = line.split()
if tmp_name != name and tmp_name != '':
for item in ls:
print tmp_name, item, ''.join(ls)
ls = []
ls.append(typex)
tmp_name = name
for item in ls: #taking care of last group
print tmp_name, item, ''.join(ls)
#============================================== |
T*******I 发帖数: 5138 | 32 是非常naive,但确实能解决问题,且逻辑简单、可操作、不会犯错,缺点只是费时和
麻烦一点。当然,我很欣赏smileguy的解决方案:简单、不会犯错,但对于没有深厚的
SAS技能的人来说是一头雾水。
【在 s******o 的大作中提到】 : 你这个根本不能叫解决问题的方法把。太naive了。
|
A*******s 发帖数: 3942 | 33 你压根就没看明白lz的问题
【在 T*******I 的大作中提到】 : 是非常naive,但确实能解决问题,且逻辑简单、可操作、不会犯错,缺点只是费时和 : 麻烦一点。当然,我很欣赏smileguy的解决方案:简单、不会犯错,但对于没有深厚的 : SAS技能的人来说是一头雾水。
|
T*******I 发帖数: 5138 | 34 我想,LZ的问题是如何把某个分类变量的各类在另一个变量上的值(可能也是分类的值)
并成一个“字符串”,是这样的吗?
事实上,我根本不明白他那样的并运算有什么实质意义?我还没见过这样构造变量的。
这样做的结果是对每一个体产生了一个主观臆想的“属性”,而这个属性的表达却是同
一类中所有个体在另一属性上的全部表达的并。
在我看来,如果typeall是一个新的变量,那么,这个变量就等同于name。他不过是换了一个新的说法而已,我们不妨把这叫着“概念的一一对应转换”。从LZ的算法逻辑可知,name和typeall这两者在整个样本空间内的分类结构及其对其它变量的意义完全相同。根据莱布尼茨的“不可分别的事物的同一性”原理,typeall或name只能二者取一,因而typeall是个多余的变量。换句话说,typeall与name之间具有100%的“同一性”,或相当于两个连续型变量之间有着100%的“共线性”。
我不知道人们能否理解我在上面所说的意思。盼交流和批评。
【在 A*******s 的大作中提到】 : 你压根就没看明白lz的问题
|
s********9 发帖数: 74 | 35 Proc datasets kill;run;quit;
data a;
input NAME $ TYPE $@@;
cards;
A 1
A 1
A 2
B 1
C 1
C 3
C 2
;
PROC SORT;
BY NAME;
data b;
set a;
by name;
retain TYPEALL;
if first.NAME then TYPEALL = TYPE;
else TYPEALL = trim(TYPEALL)||TYPE;
if last.NAME THEN OUTPUT;
keep name TYPEALL;
run;
PROC SORT;
BY NAME;
data z;
merge a b;
by name;
run;
proc print;
run;
【在 p*****o 的大作中提到】 : 如下的数据: : NAME TYPE : A 1 : A 1 : A 2 : B 1 : C 1 : C 3 : C 2 : 请问我如何可以得到这个DATA SET呢?
|