请选择 进入手机版 | 继续访问电脑版
楼主: dxystata
1135 9

[问答] 如何生成两列新变量 [推广有奖]

版主

大师

34%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
182013 个
通用积分
15204.8228
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
290409 点
帖子
5358
精华
1
在线时间
13466 小时
注册时间
2006-6-21
最后登录
2024-4-14

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

dxystata 发表于 2021-4-25 00:53:43 |显示全部楼层 |坛友微信交流群
15论坛币
微信截图_20210425004140.png

如何基于附件中的estimate数据集生成两列新变量class1和class0,如下:
class1        class0
adeno 1     squamous
large 1       squamous
small 1       squamous


谢谢!

estimate.rar (1.06 KB) 本附件包括:
  • estimate.sas7bdat

关键词:新变量 estimate Small Large Tima
  1. libname mylib "desktop";

  2. data mydata;
  3.     length class0 class1  $20;
  4.     do i=last to 1 by -1;
  5.             set mylib.estimate nobs=last point=i;
  6.                 retain class0 "";
  7.                 pos=prxmatch("/ /",left(Parameter));
  8.                 if i=last then class0=substr(Parameter, pos);
  9.                 else class1=substr(parameter,pos);
  10.                 if i^=last then output;
  11.         end;
  12.         stop;
  13.         drop pos;
  14. run;
复制代码
已有 1 人评分经验 热心指数 收起 理由
dxystata + 100 + 1 观点有启发

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

使用道具

dxystata 发表于 2021-4-26 06:28:43 |显示全部楼层 |坛友微信交流群
乐天天12300 发表于 2021-4-25 21:23
stop语句的作用?
输出的class1 class0两边好像有空格。
谢谢!

使用道具

乐天天12300 发表于 2021-4-26 09:09:35 来自手机 |显示全部楼层 |坛友微信交流群
用compress去掉所有空格

使用道具

whymath 发表于 2021-4-28 22:13:38 |显示全部楼层 |坛友微信交流群
乐天天12300 发表于 2021-4-25 21:23
你这样是否将整个数据集变成倒序了
已有 1 人评分经验 收起 理由
dxystata + 100 热心帮助其他会员

总评分: 经验 + 100   查看全部评分

使用道具

nbn523005 发表于 2021-5-10 13:33:31 |显示全部楼层 |坛友微信交流群
三种方法,1.
1.SQL过程,你去看一下SQL添加列操作;
2.生成两列组成的新数据集,然后合并。
3.直接原数据集,加两列,set语句添加变量值;


已有 1 人评分经验 收起 理由
dxystata + 100 热心帮助其他会员

总评分: 经验 + 100   查看全部评分

使用道具

dxystata 发表于 2022-11-4 09:16:31 |显示全部楼层 |坛友微信交流群
whymath 发表于 2021-4-28 22:13
你这样是否将整个数据集变成倒序了
倒序如何解决?谢谢!

使用道具

learsaas 发表于 2022-11-4 13:58:11 |显示全部楼层 |坛友微信交流群
  1. data result;
  2.         set mylib.estimate nobs=ncount;
  3.         length class1 class0 $20;
  4.         if _n_=1 then set mylib.estimate(keep=Parameter rename=(Parameter=Parameter0)) point=ncount;
  5.         class1=left(substr(left(Parameter),index(left(Parameter),' ')));
  6.         class0=left(substr(left(Parameter0),index(left(Parameter0),' ')));
  7.         if _n_ ne ncount;
  8.         drop Parameter0;
  9. run;
复制代码
已有 1 人评分经验 学术水平 热心指数 收起 理由
dxystata + 100 + 2 + 2 热心帮助其他会员

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

使用道具

whymath 发表于 2023-9-27 16:04:31 |显示全部楼层 |坛友微信交流群
这里的逻辑是什么?

使用道具

AngleeZZ 发表于 2023-9-27 16:21:37 |显示全部楼层 |坛友微信交流群
  1. data result1;  set test;  where parameter in ("adeno 1", "large 1","smarll 1");  id = whichc(parameter, "adeno 1", "large 1","smarll 1");  class1 = estimate;run;
  2. data result2;  set test;  where parameter in ("sguamous");  id = 1;  class0 = estimate;run;
  3. data result;  merge result1 result2;  by id;run;
复制代码
xxx_1.png




红色部分作新变量class1
蓝色部分作新变量class0??



已有 1 人评分经验 热心指数 收起 理由
dxystata + 100 + 1 热心帮助其他会员

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

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-16 16:24