From: CC on
Hi Tom and Barry,

Thank you so much for all of your help! It's really helpful!



On Mar 22, 5:51 am, Tom Abernathy <tom.aberna...(a)gmail.com> wrote:
> Not sure what you mean by creating variables. The original code was
> generating a RENAME command to change the name of existing variables.
> Perhaps you mean that instead of renaming the variables you would like
> to create new variables?
> In that case you could generate a series of assignment statements
> instead of the RENAME statement.  As these will be statements instead
> of just clauses for a single statement you will need to include a semi-
> colon in the generated code.
> I find that for this type of code generation work it is much easier to
> write the code to a file and then use %INC to run it. It is easer to
> understand and debug than generating code into string variables or
> macro variables.  You can read in the generated code and look at it
> before you submit it to make sure you have generated what you
> expected.
>
> filename rename temp;
> filename newvars temp;
> data _null_;
>   if 0 then set sashelp.class;
>   array nu _numeric_;
> * Write RENAME statement ;
>   file rename ;
>   put 'RENAME ' @;
>   do over nu;
>      vname=vname(nu);
>      put vname '=' 'P_' vname @;
>   end;
>   put ';' ;
> * Write assignment statements ;
>   fiel newvars;
>   do over nu;
>      vname=vname(nu);
>      put 'P_' vname '='  vname ';';
>   end;
> run;
>
> data rename;
>   set sashelp.class;
> %inc rename / source2 ;
> run;
>
> data newvars;
>   set sashelp.class;
> %inc newvars / source2 ;
> run;
>
> On Mar 21, 10:33 pm, CC <chchanghe...(a)gmail.com> wrote:
>
> > Thanks Tom!   I have one more stupid question.
>
> > As we are trying to create two variables.  Should we write--
> > renl=trim(renl);
> > vname(nu)||'=P_'||left(vname(nu));
>
> > I am not sure if we can just use "" as a blank space to create two
> > variables?  Could you clarify it for me?
> > Thanks a lot!
>
> > On Mar 21, 9:53 am, Tom Abernathy <tom.aberna...(a)gmail.com> wrote:
>
> > > ' ' is just a blank space that is used to separated the words in the
> > > generated string.
> > > || is the concatenation operator.
>
> > > On Mar 20, 8:48 pm, CC <chchanghe...(a)gmail.com> wrote:
>
> > > > Hi  I tried to prefix some variables and found someone wrote a
> > > > solution already.  But there is one part of the codes that I am not so
> > > > sure.  Could anyone answer it for me?  Thanks
>
> > > > data _null_;
> > > >  set sashelp.class (obs=1);
> > > > array nu _numeric_;
> > > > length renl $200;
> > > > renl='';
> > > > do over nu;
> > > > renl=trim(renl)||' '||vname(nu)||'=P_'||left(vname(nu));
> > > > end;
> > > > call symput('renl',renl);
> > > > run;
>
> > > > My question is what is the meaning of ||' '|| in the
> > > > renl=trim(renl)||' '||vname(nu)||'=P_'||left(vname(nu));
>
> > > > Especially for the first  ||' '|| ---  What is that for?
> > > > Also, I was wondering that where can I find the information regarding
> > > > the use of  ||' '|| in the SAS support.   I thought we have to use
> > > > ||' '||  this kind of sign when using call execute function or
> > > > something.   Not so sure why and how we use this in the data step..
>
> > > > Thank you in advance!- Hide quoted text -
>
> > - Show quoted text -
>
>