DATA Step, Macro, Functions and more

Failure to create numerical macro variables to parse SQL query

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

Failure to create numerical macro variables to parse SQL query

[ Edited ]

Beginner here.

 

I am trying to create a set of numerical values in a SAS macro with the %let command. The idea is to slice a SQL query that is giving me an overrun error (too much data that goes over the 32GB size limit) into manageable pieces and paste them back together at the end. The code is simple:

 

%let days2get = 200;


%macro pulldata;
%do i = 1 %to 20;
%let days&i = &i*10;
%put "&&days&i";
%end;
%mend pulldata;
%pulldata

 

Then the SQL query will include 

 

WHERE gp.gen_date > SYSDATE - &&days&i - 10

AND gp.gen_date < SYSDATE - &days&i

 

The problem is that the output appears to not be numerical but stored as a character: The output log is:

 

27 %let days2get = 200;
28
29
30
31 %macro pulldata;
32 %do i = 1 %to 20;
33 %let days&i = &i*10;
34 %put "&&days&i";
35 %end;
36 %mend pulldata;
37 %pulldata


"1*10"
"2*10"
"3*10"
"4*10"
"5*10"
"6*10"
"7*10"
"8*10"
"9*10"
"10*10"
"11*10"
"12*10"
"13*10"
"14*10"
"15*10"
"16*10"
"17*10"
"18*10"
"19*10"
"20*10"

 

How do I force these to be numerical?

 


Accepted Solutions
Solution
‎07-17-2018 02:12 PM
Super User
Posts: 6,934

Re: Failure to create numerical macro variables to parse SQL query

The %PUT statement writes out characters in quotes, because you added quotes in the %PUT statement.  These are different:

 

%put "&&days&i";

 

%put &&days&i;

 

As was noted, to get math to be computed, you need to apply a function.  Easiest:

 

%let days&i = %eval(&i * 10);

View solution in original post


All Replies
Super User
Posts: 2,061

Re: Failure to create numerical macro variables to parse SQL query

Are you after this?

 

%let days2get = 200;


%macro pulldata;
%do i = 1 %to 20;
%let days&i = %sysevalf(&i*10);/*correction*/
%put "&&days&i";
%end;
%mend pulldata;
%pulldata
Super User
Posts: 13,941

Re: Failure to create numerical macro variables to parse SQL query

Note: Macro variables are always text. In context of created SAS statements they may be used as, and resolve as, numeric but the actual variable is text.

Solution
‎07-17-2018 02:12 PM
Super User
Posts: 6,934

Re: Failure to create numerical macro variables to parse SQL query

The %PUT statement writes out characters in quotes, because you added quotes in the %PUT statement.  These are different:

 

%put "&&days&i";

 

%put &&days&i;

 

As was noted, to get math to be computed, you need to apply a function.  Easiest:

 

%let days&i = %eval(&i * 10);

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 59 views
  • 0 likes
  • 4 in conversation