BookmarkSubscribeRSS Feed
Karen_sas11
Calcite | Level 5
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!

 

 

12 REPLIES 12
Kurt_Bremser
Super User

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.

Karen_sas11
Calcite | Level 5

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.

Kurt_Bremser
Super User

@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.

Tom
Super User Tom
Super User

@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.
Karen_sas11
Calcite | Level 5

Thanks Tom, now I got it.

ChrisNZ
Tourmaline | Level 20

This does what you want:

data TWO;
  Y='2';
run;
%let x  =10;
%let var=Y;
data ONE;
  set TWO;
  Z=&var.||"*&x";
run;

 

Karen_sas11
Calcite | Level 5

Thahks Christ, is a period necessary in the defination of Z?

ChrisNZ
Tourmaline | Level 20

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.

 

 

FreelanceReinh
Jade | Level 19

@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).

Karen_sas11
Calcite | Level 5

 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?

FreelanceReinh
Jade | Level 19

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.

Karen_sas11
Calcite | Level 5

OK,thanks。

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 1348 views
  • 2 likes
  • 5 in conversation