SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 2242|回复: 13
打印 上一主题 下一主题

ask a question about symput()

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2012-4-18 01:03:46 | 只看该作者

ask a question about symput()

data a;
input ind $ sales @@;
  call symput("t"||compress(put(_n_,8.)), sales);
cards;
  a 36 a 58 a 90 b 61 c 27
;
run;

%macro a;
%do i=1 %to 5;
  data b; set a;
  nn=&i.;
    x=&&t&i.;
   output;
  run;
%end;
%mend;
%a
I want to assign sales values to an extra variable x, but the code above gave me a different result, and wonder if someone can help. thanks.
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2012-4-18 08:53:09 | 只看该作者

Re: ask a question about symput()

不知道你想要什么结果,目前看是%do loop没放对地方
[code:goik3eju]%macro a;
    data b;
        set a;
        %do i=1 %to 5;
            nn=&i.;
            x=&&t&i.;
            output;
        %end;
    run;
%mend;
%a[/code:goik3eju]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2012-4-19 00:42:00 | 只看该作者

Re: ask a question about symput()

ind sales x
a 36 36
a 58 58
a 90 90
b 61 61
c 27 27
that is what I want.
I knew it is very easy to get the target result(i.e. x=sales), but I just curious why the symput function and the macro gave me a different result as following:
ind sales nn x
a 36 5 27
a 58 5 27
a 90 5 27
b 61 5 27
c 27 5 27
thanks.
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2012-4-21 03:02:33 | 只看该作者

Re: ask a question about symput()

你原来的代码相当于取最后一次的值,若用option Mprint, 你会发现象;
data b;
set a;
nn=1;
x=36;
run;
data b;
set a;
nn=2;
x=58;
run;
...
data b;
set a;
nn=5;
x=27;
run;
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2012-4-21 17:44:42 | 只看该作者

Re: ask a question about symput()

学习那!!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
6#
 楼主| 发表于 2012-4-28 22:08:30 | 只看该作者

Re: ask a question about symput()

data a;
input ind $ sales @@;
ith=_n_;
cards;
a 36 a 58 a 90 b 61 c 27
;
run;

proc sort data=a;
  by ith;
run;

data b;
  set a;
  by ith;
  if last.ith then call symput('nth',ith);
run;

%macro m;
%do i =1 %to &nth;
proc sql noprint;
  select sales into <!-- s:x --><img src="{SMILIES_PATH}/icon_mad.gif" alt=":x" title="Mad" /><!-- s:x -->
  from b
  where ith=&amp;i;
quit;
%put &amp;x;
%end;
%mend;
%m;
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
7#
 楼主| 发表于 2012-4-28 22:12:44 | 只看该作者

Re: ask a question about symput()

data a;
input ind $ sales @@;
ith=_n_;
cards;
a 36 a 58 a 90 b 61 c 27
;
run;

proc sort data=a;
  by ith;
run;

data b;
  set a;
  by ith;
  if last.ith then call symput('nth',ith);
run;

%macro m;
%do i =1 %to &amp;nth;
proc sql noprint;
  select sales into : x
  from b
  where ith=&amp;i;
quit;
%put &amp;x;
%end;
%mend;
%m;
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
8#
 楼主| 发表于 2012-5-1 00:30:50 | 只看该作者

Re: ask a question about symput()

thanks MerlinZHOU.
What I wanted is using symput() function to copy variable 'sales' values, not the total observation number.
so could you help me to figure out my code problem(s)?
Thanks again.
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
9#
 楼主| 发表于 2012-5-3 21:50:18 | 只看该作者

Re: ask a question about symput()

data a;
input ind $ sales @@;
call symput((&quot;t&quot;||compress(_n_)), sales);
cards;
a 36 a 58 a 90 b 61 c 27
;
run;

%macro a;
%do i=1 %to 5;
%put x=&amp;&amp;t&amp;i;
%end;
%mend;
%a
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
10#
 楼主| 发表于 2012-5-4 04:05:43 | 只看该作者

Re: ask a question about symput()

I need a dataset with the new column x as following displayed:
ind sales x
a 36 36
a 58 58
a 90 90
b 61 61
c 27 27


but thanks again.
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-5-4 01:27 , Processed in 0.108483 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表