DATA Step, Macro, Functions and more

Why macro var created from SQL can't be used immediately in array?

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

Why macro var created from SQL can't be used immediately in array?

Hi, All,

In this case, &num was created by SQL to count the number of obs in data(have).

Instead using &num directly to create array, I have to assign the &num value to &end to make it work. Why?

The log screen was attached.

Thanks.

Joe

data have;

input year_mth @@;

cards;

201412 201411 201410

;run;

proc sql;

select count (distinct year_mth) into:num

from have

;quit;

%put check the new macro value: #

/*Why not working if use &num directly*/

data notwork;

array L (*) L01-L#

do i=01 to #

  L(i)=i;

end;

run;

/*It works when converting by let statement*/

%let end = #

data yeswork;

array L (*) L01-L&end;

do i=01 to &end;

  L(i)=i;

end;

run;

sql macro.JPG


Accepted Solutions
Solution
‎02-13-2015 10:49 AM
Trusted Advisor
Posts: 1,932

Re: Why macro var created from SQL can't be used immediately in array?

Try this:

proc sql;

select count (distinct year_mth) into:num trimmed

from have

;

View solution in original post


All Replies
Solution
‎02-13-2015 10:49 AM
Trusted Advisor
Posts: 1,932

Re: Why macro var created from SQL can't be used immediately in array?

Try this:

proc sql;

select count (distinct year_mth) into:num trimmed

from have

;

Super User
Posts: 19,855

Re: Why macro var created from SQL can't be used immediately in array?

Because the macro variable has leading spaces so what the compiler sees is:

L.            3

Use the Trimmed option in your  SQL to remove the spaces:

ie

...

Into :num Trimmed

from ...

Contributor
Posts: 61

Re: Why macro var created from SQL can't be used immediately in array?

I didn't realized that modification in sql.

Thanks.

Joe

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 215 views
  • 3 likes
  • 3 in conversation