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

Hi guys. I am new to the Macro, and a bit confuse about how macro resolve the date-time constant in keyword parameter.  This is my code. I want to give a date and number to a and b, and subset a sashelp.air first observation into a subset dataset. But I don't know what is wrong, I cannot resolve the datetime constant in my data step. Please help, thank you in advance 

%macro subsetting(a=,b=);

data subset;
set sashelp.air;
if date ="&a" and air ="&b";
run;
%mend;
%subsetting(a='01jan94'd,b=112);
1 ACCEPTED SOLUTION

Accepted Solutions
sustagens
Pyrite | Level 9

Your macro program is fine, it's just an issue of mismatched data type.

Since variables date and air are numeric, you also have to supply numeric values to it.

 

The parameters you are passing are already numeric, but if you enclose them in quotes it will be processed as a literal string (character).

 

Get rid of the double quotes and it should work. 

if date =&a and air =&b;

Just to note, you are working with a "date" constant (i.e. '01Jan1994'd), not  "date-time" (i.e. '01Jan1994:00:00:00'dt)

View solution in original post

3 REPLIES 3
sustagens
Pyrite | Level 9

Your macro program is fine, it's just an issue of mismatched data type.

Since variables date and air are numeric, you also have to supply numeric values to it.

 

The parameters you are passing are already numeric, but if you enclose them in quotes it will be processed as a literal string (character).

 

Get rid of the double quotes and it should work. 

if date =&a and air =&b;

Just to note, you are working with a "date" constant (i.e. '01Jan1994'd), not  "date-time" (i.e. '01Jan1994:00:00:00'dt)

RichardDeVen
Barite | Level 11

Is SASHELP.AIR the DATE variable is a number type containing SAS DATE values formatted to display as MONYY5.  The AIR variable is a number type.

 

IF and WHERE statements deal with the underlying date value.  A date value is the number of days since 01JAN1960.  A date value can be presented to the system as a date LITERAL.  The construct is "ddMONyy"Dor "ddMONyyyy"D.  Some other constructs are similar variations such as "dd-MONyyyy"Dand "dd-MON-yyyy"D.  Single quotes can be used in place of double quotes in a date literal.

 

So, you are passing the macro a sequence of characters (nominally called a string in macro, but it is not a string like in C or Java) that represents a date literal when resolved in the context of source code of a DATA step programming statement.

 

The B parameter is being passed characters that represent a number when resolved in the context of programming source code.

 

What this all means is that you should NOT double quote the resolution of either parameter A or parameter B.  

 

Use the MPRINT session option to have macro generated code appear in the LOG.

 

%macro subsetting(a=,b=);

data subset;
set sashelp.air;
if date = &a and air =&b;
run;
%mend;

options mprint;

%subsetting(a='01jan94'd,b=112)
----- LOG -----
16   %subsetting(a='01jan94'd,b=112)
MPRINT(SUBSETTING):   data subset;
MPRINT(SUBSETTING):   set sashelp.air;
MPRINT(SUBSETTING):   if date = '01jan94'd and air =112;
MPRINT(SUBSETTING):   run;

NOTE: There were 144 observations read from the data set SASHELP.AIR.
NOTE: The data set WORK.SUBSET has 0 observations and 2 variables.
NOTE: DATA statement used (Total process time):

 

   

shawn123
Obsidian | Level 7
Thank you for the reply. Learn a lot!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 392 views
  • 1 like
  • 3 in conversation