SAS中文论坛

标题: ask a question about symput() [打印本页]

作者: shiyiming    时间: 2012-4-18 01:03
标题: 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.
作者: shiyiming    时间: 2012-4-18 08:53
标题: 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]
作者: shiyiming    时间: 2012-4-19 00:42
标题: 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.
作者: shiyiming    时间: 2012-4-21 03:02
标题: 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;
作者: shiyiming    时间: 2012-4-21 17:44
标题: Re: ask a question about symput()
学习那!!
作者: shiyiming    时间: 2012-4-28 22:08
标题: 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;
作者: shiyiming    时间: 2012-4-28 22:12
标题: 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;
作者: shiyiming    时间: 2012-5-1 00:30
标题: 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.
作者: shiyiming    时间: 2012-5-3 21:50
标题: 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
作者: shiyiming    时间: 2012-5-4 04:05
标题: 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.
作者: shiyiming    时间: 2012-7-15 21:55
标题: Re: ask a question about symput()
集思广益!!!
作者: shiyiming    时间: 2012-7-18 01:59
标题: Re: ask a question about symput()
you may use symget to do it.
%macro a;
data b;set a;
x=symget(&quot;t&quot;||compress(put(_n_,3.)));
run;
%mend;
%a
作者: shiyiming    时间: 2012-7-19 05:34
标题: Re: ask a question about symput()
good try.
there are many ways to copy sales to x as I said before, but I am just curious what is wrong with my code(As you can see, it looks good and reasonable).
作者: shiyiming    时间: 2012-7-19 21:08
标题: Re: ask a question about symput()
After macro compiling, the program processing should be that the following codes were executed sequentially:
data b; set a; nn =1;x =36;output;run;
data b; set a; nn =2;x =58;output;run;

data b; set a; nn =5;x =27;output;run;
After being continuously overwritten, the set b in the last round is the final one: nothing more than assigning number to both variables of nn and x.
you could see it more clearly by changing data b; &gt;&gt;&gt;data;
jingju




欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2