BookmarkSubscribeRSS Feed
Jabari
Calcite | Level 5

What I\m trying to do seems simple but I've been trying to figure it out for the last hour and still nothing.  I have a variable called NCCT_Date.  What ever I pass to this variable I would like to populate every row of an already create dataset and call the column the data is getting passed to Rpt_Date.  The code I'm using is shown below.  When I run the code the Rpt_Date column is created but the 'Fall2021' data is not getting passed.

 

%let NCCT_Date = Fall2021;

 

data work.sampletbl;

set work.sampletbl;

Rpt_Date = &NCCT_Date;

run;

7 REPLIES 7
AMSAS
SAS Super FREQ

Why use a macro variable when you don't need to?
Assuming Rpt_Date is a character variable then you need to quote the macro variable

 

data work.sampletbl;
	set work.sampletbl;
	Rpt_Date = &NCCT_Date;
	Rpt_Date1 = "Fall2021" ;
	Rpt_Date2 = "&NCCT_Date";
run;
Jabari
Calcite | Level 5

I need the macro because the "Fall2021" is used in several other places in the code.  But putting quotes around the NCCT_Date works.  I didnt think you could do that .

PaigeMiller
Diamond | Level 26

Macro variables perform text substitution when the code is executed. The value of the macro variable replaces the actual macro variable in your code upon execution. And the result MUST be valid working SAS code.

 

SO your line of code is:

 

Rpt_Date = &NCCT_Date;

and when you execute the code, it becomes

 

 Rpt_Date = Fall2021;

So let me ask you, is this valid SAS code? Does it work, does it do what you want? If there is a variable in your SAS dataset named Fall2021, this works. But it doesn't assign the value of a text string Fall2021 to Rpt_Date, because you haven't created working SAS code when the macro variable value is substituted into your code. Do you see why this won't work to do what you want?

 

Also, you should first always create working SAS code without macros and without macro variables. It appears you didn't do that. This is always a mandatory first step ... if you don't have working SAS code, then code with macro variables will never work either.

--
Paige Miller
Jabari
Calcite | Level 5

I tried adding quotes to the macro so it could pass as 'Fall2021'.  When I check this in the log it is resolving as expected but still will not pass correctly to the data step.  I guess I'm asking what code do I need to write to pass 'Fall2021' correctly.


NCCT_Date2 = %quote(%')&NCCT_Date%quote(%') ;

%put &NCCT_Date2;

AMSAS
SAS Super FREQ

You need to understand how macro variables resolve.

Here's some good reading 

Macro Variables Defined by Users

Using Macro Variables

 

Here's the example with some comments added, but I recommend you read the documentation 

 

512  options symbolgen mprint ;
513  %let NCCT_Date = Fall2021;
514
515  data _null_ ;
516      /* Resolves to
517      Rpt_Date = Fall2021;
518      */
519      Rpt_Date = &NCCT_Date;
SYMBOLGEN:  Macro variable NCCT_DATE resolves to Fall2021
520      /* No macro resolution */
521      Rpt_Date1 = "Fall2021" ;
522      /* Resolves to
523      Rpt_Date2 = "Fall2021" ;
524      */
525      Rpt_Date2 = "&NCCT_Date";
SYMBOLGEN:  Macro variable NCCT_DATE resolves to Fall2021
526      put rpt_date= rpt_date1= rpt_date2= ;
527  run;

NOTE: Variable Fall2021 is uninitialized.
Rpt_Date=. Rpt_Date1=Fall2021 Rpt_Date2=Fall2021
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

 

 

ballardw
Super User

@Jabari wrote:
Spoiler
I tried adding quotes to the macro so it could pass as 'Fall2021'.  When I check this in the log it is resolving as expected but still will not pass correctly to the data step.  I guess I'm asking what code do I need to write to pass 'Fall2021' correctly.


NCCT_Date2 = %quote(%')&NCCT_Date%quote(%') ;

%put &NCCT_Date2;






If you are using a macro function like %quote in a data step expect unexpected results. First there is a perfectly good non-macro function Quote (not needed).

Second if you use macro variables frequently you will find that attaching the quotes to the macro variable will quite often be more complex to debug than using the basic "&macrovariable." when trying to use it in data step, procedure or other statements.

Third you should show entire code of things that "don't work" and likely it should be from the LOG with all the notes and messages. For example your code above does not include a %let and does not appear in a data step so generates errors when attempting the assignment to NCCT_Date2.

 

Tom
Super User Tom
Super User

In general you probably do not want the quote characters in the macro variable. But it depends how you are using the macro variable.  Macro expressions will resolve inside of double quotes so just use those.

data want;
   set have;
   sas_variable = "&macro_variable":
run;

If you really need to add single quotes, perhaps because you are using the value in pass through SQL, then use %BQUOTE().

proc sql;
connect using mylib ;
create table want as select * from connection to mylib
(select * from sql_table where sql_variable = %bquote('&macro_variable') 
)
;
quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 6839 views
  • 2 likes
  • 5 in conversation