data two;
y='2';
run;
%let x=10;
%let var=y;
data one;
set two (keep=&var);
z=&var*&x;
run;
Hello,
As I know, macro variable can be defined with %let statement, and everything is text in macro variable. So in the above code, variable z returns the value should by text value y multiples text value 10, but actually returns the numeric value 20.
I didn't see any function above that can convert character value to numeric value.
Anyone can explain it for me please?
Many thanks!
When SAS finds a character variable in a place where a numeric value is expected, it makes an automatic typecast. You will get a NOTE in the log about this, and further NOTEs if the contents of the character variable cannot be converted.
Thank you Kurt, I didn't really realize that there is a note in the log about this. I went back the log, and there was.
@Karen_sas11 wrote:
Thank you Kurt, I didn't really realize that there is a note in the log about this. I went back the log, and there was.
Maxim 2. Always read the log, even if a piece of code (seemingly) works.
@Karen_sas11 wrote:
data two; y='2'; run; %let x=10; %let var=y; data one; set two (keep=&var); z=&var*&x; run;
Hello,
As I know, macro variable can be defined with %let statement, and everything is text in macro variable. So in the above code, variable z returns the value should by text value y multiples text value 10, but actually returns the numeric value 20.
I didn't see any function above that can convert character value to numeric value.
Anyone can explain it for me please?
Many thanks!
Not quite. The macro processor will replace the references to the macro variables by their (text) values and then the resulting CODE (which is of course always text) is run by SAS.
So you told SAS to run this data step.
data one;
set two (keep=y);
z=y*10;
run;
So you can see that your SAS code is asking SAS to multiple Y times the constant value 10. Since Y is a character variable SAS will have to convert its values to a number first in order to do that. The SAS log will show you that.
436 data one; 437 set two (keep=&var); 438 z=&var*&x; 439 run; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 438:1 NOTE: There were 1 observations read from the data set WORK.TWO. NOTE: The data set WORK.ONE has 1 observations and 2 variables.
Thanks Tom, now I got it.
This does what you want:
data TWO;
Y='2';
run;
%let x =10;
%let var=Y;
data ONE;
set TWO;
Z=&var.||"*&x";
run;
Thahks Christ, is a period necessary in the defination of Z?
Try it.
You'll see that Z.= is invalid syntax.
Periods can be used only to mark the end of macro variable names. Not for data set variables.
Since your understanding of the SAS language seems so basic, I would strongly suggest that you do not touch the (much more finicky) macro language for now. I'll only create headaches for yourself.
@ChrisNZ: I think Karen referred to the period you used after "&var" whereas she had omitted the period in her own code.
@Karen_sas11: In this case the answer is: No, the period is just optional. It would be necessary if what follows it could be interpreted as the macro variable name's continuation (e.g. &var.max, &var.1) or was itself a period (e.g. &var..sum).
Thanks Freelance, that's what I am asking.
So far I know the period is frequently used in the alias that refer to the source table in SQL , two-dimension naming, and format/informat, I think.....are they the main uses of a period?
Yes, these are typical occasions where the period is required. But all these are cases where an adjacent period with another meaning makes the (additional) period necessary.
OK,thanks。
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.