DATA Step, Macro, Functions and more

Count with left

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 85
Accepted Solution

Count with left

Hi All,

I am trying to create macro variables using proc sql within a macro.But the code does not work. its pretty simple code but I do not understand why its not working.

data have;

input var;

datalines;

12

34

8

79

84

;

run;

%macro m1;

proc sql noprint;

select count(var) into :cnt from have ;

quit;

Proc sql noprint;

select var into :val1 - :val%left(&cnt) from have;

Quit;

%mend;

%m1;

%put &val1 &val2 &val3 &val4 &val5;


Accepted Solutions
Solution
‎06-11-2013 11:24 PM
Respected Advisor
Posts: 3,124

Re: Count with left

There are two ways to make the macro variables global:

1) Remove macro definition completely.

2) Using %global statement

Otherwise they are local, they still can be printed out within your macro though.

Haikuo

View solution in original post


All Replies
Solution
‎06-11-2013 11:24 PM
Respected Advisor
Posts: 3,124

Re: Count with left

There are two ways to make the macro variables global:

1) Remove macro definition completely.

2) Using %global statement

Otherwise they are local, they still can be printed out within your macro though.

Haikuo

Frequent Contributor
Posts: 138

Re: Count with left

what is the error your getting? I think you cannot use %left function . What you can do %let cnt=%trim(&cnt) before the second query.

Super User
Posts: 5,256

Re: Count with left

There is no problem with the %left. This is as Haikuo already stated a problem with local vs global macro variables.
Removing the macro definition seems to the most reasonable action, having the code wrapped up inside a macro does not add anything...

Data never sleeps
Super Contributor
Posts: 276

Re: Count with left

Yes ,You Have to mention %global in your macro as said by .

or else you have to create all the macro variables  with dummy values before the macro.

like

data have;

input var;

datalines;

12

34

8

79

84

;

run;

%let Val1=0;

%let Val2=0;

%let Val3=0;

%let Val4=0;

%let Val5=0;

%macro m1;

proc sql noprint;

select count(var) into :cnt from have ;

quit;

Proc sql noprint;

select var into :val1 - :val%left(&cnt) from have;

Quit;

%mend;

%m1;

%put &val1 &val2 &val3 &val4 &val5;




But i always prefer to use  %Global.



Regards.

Sanjeev.K

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 235 views
  • 3 likes
  • 5 in conversation