楼主: dxystata
2483 11

如何把数据集中的数值型全部变为字符型2 [推广有奖]

版主

已卖: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 发表于 2014-1-26 20:48:03 |AI写论文
200论坛币
proc format;
value yf 1="m"
         2="f";
run;

data aaa;
input x z$ y b$ a;
label y="sex";
attrib a format=MMDDYY10.;
cards;
1 b 1 c 1
2 e 2 d 2
;
run;


/* yf为y的format
    a的format为 MMDDYY10.
*/

数据集中有字符型 也由数值型,目的把数据集中的数值型全部变为字符型
要求:1.变量名不变
         2.变量顺序不变。
         3.如果存在对应的format,则变成format的值
         4.最好数据集名也不变。
         5.用macro实现。

程序要通用哦!希望得到的结果类似下图
2014-01-26_204553.png

谢谢!


最佳答案

邓贵大 查看完整内容

楼主这样的高人应该雇个人帮你搞定这样的小事。
关键词:数据集 字符型 数值型 format FORMA 如何 最好

本帖被以下文库推荐

沙发
邓贵大 发表于 2014-1-26 20:48:04
  1. proc format;
  2. value yf 1="m"
  3.          2="f";
  4. run;

  5. data aaa;
  6. input x z$ y b$ a;
  7. label y="sex";
  8. attrib a format=MMDDYY10.
  9.         y format=yf.;
  10. cards;
  11. 1 b 1 c 1
  12. 2 e 2 d 2
  13. ;
  14. run;

  15. %macro tochar(in=, out=);
  16. %local i j N tmp1;
  17. data;stop;run;        %let tmp1 = %substr(&syslast,6);

  18. proc contents data=&in out=&tmp1 noprint;
  19. proc sql noprint;
  20.         select count(*) into :N from &tmp1;
  21.         %let N=&N;
  22.         %do i=1 %to &N;
  23.                 %local varname&i vartype&i varformat&i;
  24.         %end;
  25.         select name, type into :varname1-:varname&N, :vartype1-:vartype&N
  26.                 from &tmp1 order by varnum;
  27. quit;
  28. data _null_;
  29.         if 0 then set ∈
  30.         %do i=1 %to &N;
  31.                 call symputx("varformat&i", vformat(&&varname&i));
  32.         %end;
  33.         stop;
  34. run;
  35. proc sql;
  36. create table &out as
  37.         select
  38.         %do i=1 %to &N;
  39.                 %if &&vartype&i=1 %then put(&&varname&i, &&varformat&i) as &&varname&i; %else &&varname&i;
  40.                 %if &i<&N %then ,;
  41.         %end;
  42.         from &in;
  43. quit;
  44. proc datasets nolist;
  45.         delete &tmp1;
  46. quit;
  47. %mend;

  48. %tochar(in=aaa, out=bbb)
复制代码
楼主这样的高人应该雇个人帮你搞定这样的小事。
已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
dxystata + 100 + 3 + 3 + 1 热心帮助其他会员

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

Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

藤椅
dxystata 发表于 2014-1-26 20:53:41
谢谢!

板凳
dxystata 发表于 2014-1-27 03:44:21
邓贵大 发表于 2014-1-27 03:40
楼主这样的高人应该雇个人帮你搞定这样的小事。
谢谢邓兄!春节快乐!
论坛高手很多,代码写得非常好!多请教,多学习!

报纸
dxystata 发表于 2014-1-27 03:57:33
macro中如何先对日期格式进行判断呢? 从而更改为另外的日期格式。

地板
邓贵大 发表于 2014-1-27 07:46:59
Why don't you just assign the desired Date format to the variable in the first place? It's not worth the extra effort coding a macro.
There are only two data types in SAS - chars or numbers, but not dates. Dates are simply numbers ranging from A.D. 1582 to A.D. 19,900. It's also a lot of hassle to tell if a format is a date format in SAS code because of the existence of numerous built-in data formats and the customized picture/nesting (date) formats.
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

7
playmore 发表于 2014-1-27 09:15:49
如果想要解决问题,但不需要解决的漂亮
可以先把数据集导出至外部的txt或csv文件
然后再导入至SAS
导入的时候设置所有变量都是字符型就好了
已有 1 人评分学术水平 热心指数 收起 理由
dxystata + 1 + 1 好的意见建议

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

playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

8
farmman60 发表于 2014-1-27 10:09:22

Not macro.

proc format;
value yf 1="m"
         2="f";
run;

data aaa;
input x z$ y b$ a;
label y="sex";
format y yf.;
attrib a format=MMDDYY10.;
cards;
1 b 1 c 1
2 e 2 d 2
;
run;


proc sql;
  select name into:varslist separated by ' ' from sashelp.vcolumn where libname='WORK' and memname='AAA';

data _null_;
   set sashelp.vcolumn;
   where libname='WORK' and memname='AAA';
   if type='num' then do;
      if not missing(format) then call symput("fmt",trim(left(format)));
                else call symput("fmt",'8.');
                call symput("lab",trim(left(label)));
             call execute(
'data aaa;
retain &varslist;
set aaa ;
   _'||name||'=put( '||strip(name)||',&fmt);
drop '||name||';
label _'||strip(name)||'="&lab  ";

rename _'||strip(name)||'='||strip(name)||';
run;');
      end;
  run;


已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
dxystata + 100 + 1 + 1 热心帮助其他会员
Tigflanker + 1 + 1 + 1 交织感很强。

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

9
dxystata 发表于 2014-3-2 23:17:52
farmman60 发表于 2014-1-27 10:09
Not macro.

proc format;
good!

10
dxystata 发表于 2014-3-5 03:12:41
farmman60 发表于 2014-1-27 10:09
Not macro.

proc format;
NOTE: 逻辑库“MAPSGFK”中的映射数据集基于 GfK GeoMarketing
      中的数字映射,且要受其版权控制。有关详细信息,请参见
      http://support.sas.com/mapsonline/gfklicense
NOTE: 从数据集 SASHELP.VCOLUMN. 读取了 5 个观测
      WHERE (libname='WORK') and (memname='AAA');
NOTE: “DATA 语句”所用时间(总处理时间):
      实际时间          11.37 秒
      CPU 时间          1.35 秒

能否不用SASHELP.VCOLUMN? 谢谢!

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

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