楼主: dxystata
1699 6

[有偿编程] 根据字符变量x中取值的顺序,得到新的变量y· [推广有奖]

版主

已卖:302份资源

大师

37%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
183395 个
通用积分
15333.1475
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
298627 点
帖子
5586
精华
1
在线时间
13632 小时
注册时间
2006-6-21
最后登录
2025-12-22

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

楼主
dxystata 发表于 2019-7-12 23:50:20 |AI写论文
10论坛币
  1. data aaa;
  2. input x$;
  3. cards;
  4. a
  5. b
  6. a
  7. c
  8. b
  9. c
  10. c
  11. .
  12. ;
  13. run;
复制代码
先根据字符变量x中取值的顺序,得到新的变量y,按照顺序分别为1 2 3。
x  y
a  1
b  2
a  1
c  3
b  2
c  3
c  3
    .

不要用if x="a" then y=1;  根据顺序判断。谢谢!

关键词:字符变量 Input cards card Data

沙发
Bugjay 发表于 2019-7-13 10:55:19
用first判断是否是新数据,再加一个自增数值

藤椅
glmswufe 发表于 2019-7-14 10:30:51
data aaa;
input x$;
cards;
a
b
a
c
b
c
c
.
;
run;
data aaa;
set aaa;
id=_n_;
run;
proc sort data=aaa out=aaasort;
by x;
run;
data aaasort;
set aaasort;
by x;
if missing(x) then y=.;
else if not missing(x) then  if first.x=1 then y+1;
output;
run;
proc sort data=aaasort out=aaa;
by id;
run;
data aaa;
set aaa;
drop id;
run;



已有 2 人评分经验 热心指数 收起 理由
eijuhz + 40 精彩帖子
dxystata + 100 + 1 热心帮助其他会员

总评分: 经验 + 140  热心指数 + 1   查看全部评分

板凳
wwang111 发表于 2019-7-24 22:57:31
  1. proc sql;
  2. create table fmt1 as
  3. select x, min(obs) as n
  4. from
  5. (select x, monotonic() as obs
  6. from aaa
  7. where x ne '')
  8. group by 1
  9. order by 2;
  10. quit;

  11. data fmt2;
  12. set fmt1;
  13. label+1;
  14. rename x=start;
  15. fmtname="$order";
  16. type="C";
  17. run;

  18. proc format cntlin=fmt2;
  19. run;

  20. data wanted;
  21. set aaa;
  22. y=input(put(x,$order.),best.);
  23. run;
复制代码

已有 2 人评分经验 热心指数 收起 理由
eijuhz + 40 精彩帖子
dxystata + 100 + 1 热心帮助其他会员

总评分: 经验 + 140  热心指数 + 1   查看全部评分

报纸
gudingji 在职认证  发表于 2019-12-24 13:16:33
  1. proc sql;
  2.     create table b as select distinct x from  aaa where missing(x)=0;
  3. quit;
  4. data b;set b;index=_n_;run;
  5. proc sql;
  6. create table a2 as
  7. select a.x,b.index from aaa as a
  8. left join b on a.x=b.x;
  9. quit;
复制代码
已有 2 人评分经验 热心指数 收起 理由
eijuhz + 40 精彩帖子
dxystata + 100 + 1 热心帮助其他会员

总评分: 经验 + 140  热心指数 + 1   查看全部评分

地板
xixixixixiixixi 发表于 2020-2-20 23:55:28
data aaa;
input x$;
cards;
a
b
a
c
b
c
c
.
;
run;

data dataset1;
      set aaa;
          if x ne '' then rank='1';
          else rank='2';  
run;


proc sql;
      create table dataset2 as
          select distinct *
          from dataset1
      order by rank;
quit;


data dataset3;
      set dataset2;
      y=_n_;
          if x='' then y=.;
          drop rank;
run;
          
data dataset4;
      set aaa;
      rank=_n_;
run;

proc sql;
      create table dataset5 as
          select a.x,b.y
          from dataset4 as a left join dataset3 as b
          on a.x=b.x
      order by rank;
quit;

7
Cecilia_Xi 在职认证  发表于 2020-2-27 23:05:53
  1. /*综合了大家的答案,并做了一定的简化,一共3种实现方法*/
  2. data aaa;
  3. input x$;
  4. cards;
  5. a
  6. b
  7. a
  8. c
  9. b
  10. c
  11. c
  12. .
  13. ;
  14. run;


  15. /*方法1【by】 25 行*/
  16. data by1;*用于最后一步还原原观测顺序;
  17. set aaa;
  18. id=_n_;
  19. run;

  20. proc sort data=by1 out=by2;*下一步为同一变量值取相同的值;
  21. by x;
  22. run;

  23. data by3;
  24. set by2;
  25. by x;*与上一步对应;
  26. if missing(x) then y=.;
  27. else if first.x=1 then y+1;
  28. output;
  29. run;

  30. proc sort data=by3 out=by4;
  31. by id;*与step1对应;
  32. run;

  33. data wanted1;
  34. set by4;
  35. drop id;
  36. run;



  37. /*方法2【Format Procedure:Proc Format Statement--ctlnin option】:*Creates user-specified formats and informat for variables. 17 行*/
  38. proc sql;
  39. create table fmt1 as
  40. select distinct x
  41. from aaa
  42. where x ne '';
  43. quit;

  44. data fmt2;/*Input Control dataset:将符合标准的sas数据集(包含固定字段的数据集,所必须的字段名为fmtname,start,label)*/
  45. set fmt1;
  46. label+1;
  47. rename x=start;/*start就是指input值,label就是output的值*/
  48. fmtname="$order";
  49. type="C";/*C for character format, N for numeric format, I specifies a numericinformat.J specifies a characterinformat.*/
  50. run;

  51. proc format cntlin=fmt2;*cntlin:import control;
  52. run;

  53. data wanted2;
  54. set aaa;
  55. y=input(put(x,$order.),best.);
  56. run;


  57. /*方法3【Proc sql】:14行*/
  58. proc sql;
  59.       create table sql1 as
  60.           select *,monotonic() as y
  61.           from (select distinct x from aaa)
  62.           where x ne '';
  63. quit;/**proc sql中,无_n_,但可使用monotoni():自增函数(sql/data):The function counts the number of times
  64.       it is called and returns an ascending sequence of integers, starting at 1*/
  65.          
  66. proc sql;
  67.       create table wanted3 as
  68.           select a.x,b.y
  69.           from (select x, monotonic() as rank from aaa) as a left join sql1 as b
  70.           on a.x=b.x
  71.       order by rank;
  72. quit;
复制代码
已有 1 人评分经验 学术水平 热心指数 收起 理由
dxystata + 100 + 1 + 1 热心帮助其他会员

总评分: 经验 + 100  学术水平 + 1  热心指数 + 1   查看全部评分

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-28 22:18