BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
forumsguy
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

4 REPLIES 4
Haikuo
Onyx | Level 15

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

manojinpec
Obsidian | Level 7

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.

LinusH
Tourmaline | Level 20

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
kuridisanjeev
Quartz | Level 8

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1038 views
  • 3 likes
  • 5 in conversation