| Date: | Tue, 23 Aug 2005 15:33:42 -0400 |
| Reply-To: | Nishant Dholakia <nishant.dholakia@GMAIL.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Nishant Dholakia <nishant.dholakia@GMAIL.COM> |
| Subject: | Re: Help: weird macro variable reference result...Thx |
|
| In-Reply-To: | <200508172315.j7HNF0TV025863@listserv.cc.uga.edu> |
| Content-Type: | text/plain; charset=ISO-8859-1 |
Hi,
%let y = abc;
x = &y;
here x will get the value abc ie x = abc. So SAS assumes that abc is a
variable in itself however when you do.
x = "&y"
x = "abc" hence SAS compiler comes to know its a character value.
For numeric values SAS performs the calculations implicitly.
%let y =5;
x = y
is going to give you x = 5 and here 5 is a numeric value hence its not
considered as a variable.
I hope this helps.
PS : I did not go through the whole thread so I might be answering
something totally different from what you were asking.
CHEERS
On 8/17/05, Ching Fang Chi <chingfang@gmail.com> wrote:
>
> I was reading document about macro variable.
> So I tried the following code.
> =================================
> %let name = derek;
>
> data try1;
> people1 = &name;
> %put &name;
> put people1=;
>
> %let name = karen;
>
> people2 = &name;
> %put &name;
> put people2=;
> run;
>
> proc print data=try1;
> run;
> =================================
> In the log the value of name were shown correctly: derek and karen.
> But in the output window, the result was:
> Obs people1 derek people2 karen
> 1 . . . .
>
> I guess the macro variable reference resolvation was wrong.
> So I gave a second try.
> =================================
> %let name = derek;
>
> data try2;
> people1 = "&name"; /*add quotation mark here*/
> %put &name;
> put people1=;
>
> %let name = karen;
>
> people2 = "&name"; /*add quotation mark here*/
> %put &name;
> put people2=;
> run;
>
> proc print data=try2;
> run;
> =================================
> I added quotation marks in the assignment statements for variable people1
> and people2. When I checked the output result, it showed:
> Obs people1 people2
> 1 derek karen
>
> It seemed that after I added the quatation marks the value defined by %let
> statement could be resolved correctly.
>
> Then I gave a third try.
> =================================
> %let name = 5; /*change the value from derek to 5 */
>
> data try3;
> people1 = "&name"; /*still keep the quotation mark*/
> %put &name;
> put people1=;
>
> %let name = 10;
>
> people2 = "&name"; /*still keep the quotation mark */
> %put &name;
> put people2=;
>
> run;
>
> proc print data=try3;
> run;
> =================================
> I changed the value of macro variavle name from derek to 5.
> The result was similar to the second one:
> Obs people1 people2
> 1 5 10
>
> which also seemed right for me. I decided to take off the quotation
> marks like the one I did in the first try.
> =================================
> %let name = 5;
>
> data try4;
> people1 = &name; /*take off the quotation mark*/
> %put &name;
> put people1=;
>
> %let name = 10;
>
> people2 = &name; /*take off the quotation mark*/
> %put &name;
> put people2=;
>
> run;
>
> proc print data=try4;
> run;
> =================================
> To my surprise the result was the same as the third try.
> Obs people1 people2
> 1 5 10
>
> I was very confused about the result since quotation mark was not
> needed. Then I went on another try:
> =================================
> %let name = derek; /*change back the value from 5 to derek */
>
> data try5;
> people1 = &name;
> %put &name;
> put people1=;
>
> %let name = 10;
>
> people2 = &name;
> %put &name;
> put people2=;
>
> run;
>
> proc print data=try5;
> run;
> =================================
> The result was:
> Obs people1 derek people2
> 1 . . 10
>
> The result was good for variable peopl2.
> If I added back the quotation mark to the assignment statement
> for people1, the result would be:
> =================================
> %let name = derek;
>
> data try6;
> people1 = "&name"; /*add back the quotation mark */
> %put &name;
> put people1=;
>
> %let name = 10;
>
> people2 = &name;
> %put &name;
> put people2=;
>
> run;
>
> proc print data=try6;
> run;
> =================================
> Obs people1 people2
> 1 derek 10
>
> I don't know if the weird result came from the incorrect usuage of
> macro variable reference in an assignment statement(like people1 = &name).
> Does quotation mark really matter? or I misunderstood about macro variable
> reference? It seemed like if I assign a string value, I would have to use
> quotation mark; If I assign a numeric value then quotation mark is not
> necessary.
>
> But I also read that the value assigned to a macro variable is treated as
> character string. So I can't figure out how macro reference is resolved
> actually. Can anyone help me solving the problem?
>
> Thanks again for any comments.
>
> Cheers,
> Ching Fang
>
--
Nishant H. Dholakia
607 262 0860
"its your attitude not your aptitude that determines your altitude"
|