BookmarkSubscribeRSS Feed
fsuzhang
Fluorite | Level 6

I have the following macro definition and be stuck in middle

 

%let From_Date = JAN07;

%let new_input=%sysfunc(inputn("JAN07"d, MONYY5.));
%let new_input=%sysfunc(inputn(&From_Date., MONYY5.));
%put &new_input.;


data test;
do k=0 to 10;
y= %sysfunc(intnx(month, &new_input., k , 'e'));
output;
end;
run;

 

1. the second clause can not read in 'JAN07"d', the 3th clause can output correctly, why this happen.

2. in loop, k can not be recognized by intnx function as a number, the error is

 

"Argument 3 to function INTNX referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number."

 

Thanks,

 

12 REPLIES 12
PaigeMiller
Diamond | Level 26

Macro variables SHOULD NOT be formatted.

 

%let From_Date = %sysevalf('01JAN07'd); /* Un-format the date */
%let new_input=&From_Date;
%put &new_input.;

data test;
do k=0 to 10;
y= intnx('month', &new_input., k , 'e');
output;
end;
format y date7.;
run;
--
Paige Miller
fsuzhang
Fluorite | Level 6

Thank, y value works. however,
I need to convert it back to date, as below

data test2;
do k=0 to 10;
/*y= %sysfunc(intnx(month, &new_input., k , 'e'));*/
y=intnx('month', &new_input., k , 'e');
year=%sysfunc(putn(y, MONYY5.));
file=rec_&year.;
output;
end;
run;

but this time the %sysfunc() told me y is not a number.

PaigeMiller
Diamond | Level 26

@fsuzhang wrote:

Thank, y value works. however,
I need to convert it back to date, as below

data test2;
do k=0 to 10;
/*y= %sysfunc(intnx(month, &new_input., k , 'e'));*/
y=intnx('month', &new_input., k , 'e');
year=%sysfunc(putn(y, MONYY5.));
file=rec_&year.;
output;
end;
run;

but this time the %sysfunc() told me y is not a number.


Important concept: get the DATA step and other SAS code to work properly without macros and without macro variables for one instance, such as for a specific date, hard-coded. Once you get that to work properly without macros and without macro variables, then you have a chance to get it to work with macros and with macro variables. If you can't get that to work properly without macros and without macro variables, then it will never work with macros and macro variables.

 

So, show us code that works with one hard-coded date, and no macros and no macro variables.

 

As written, your DATA step is meaningless. Explain what it is supposed to be doing.

 

And please don't post the same question in two places.

--
Paige Miller
fsuzhang
Fluorite | Level 6
Thank,
I need to convert it back to date, as below

data test2;
do k=0 to 10;
/*y= %sysfunc(intnx(month, &new_input., k , 'e'));*/
y=intnx('month', &new_input., k , 'e');
year=%sysfunc(putn(y, MONYY5.));
file=rec_&year.;
output;
end;
run;

but this time the %sysfunc() told me y is not a number.
PaigeMiller
Diamond | Level 26

Generally you don't need %SYSFUNC when data step operations will work.

 

But it's not really clear what you are doing or what the purpose of this line is:

file=rec_&year.;

It looks like you are creating a new variable FILE which is equal to some other variable, but there are no variables named rec_xxxxx, so it is a meaningless line at this point in time. Please explain what this data step is supposed to be doing.

--
Paige Miller
Tom
Super User Tom
Super User

@fsuzhang wrote:
Thank,
I need to convert it back to date, as below

data test2;
do k=0 to 10;
/*y= %sysfunc(intnx(month, &new_input., k , 'e'));*/
y=intnx('month', &new_input., k , 'e');
year=%sysfunc(putn(y, MONYY5.));
file=rec_&year.;
output;
end;
run;

but this time the %sysfunc() told me y is not a number.

That is just gibberish code. You are setting the numeric dataset variable Y to a date value.  You then create a character dataset variable named YEAR that has a string like 'JAN07'.  You then set the dataset variable FILE to the value of dataset variable whose name was created by appending the value of the macro variable YEAR to the letters REC_.  

You do not show where you are creating the macro variable YEAR.  Nor are you creating any variables whose names start with REC_, no matter what value the macro variable YEAR had when the data step was compiled.

 

What is it that you are trying to do?

fsuzhang
Fluorite | Level 6
Thank,
I need to convert it back to date, as below

data test2;
do k=0 to 10;
/*y= %sysfunc(intnx(month, &new_input., k , 'e'));*/
y=intnx('month', &new_input., k , 'e');
year=%sysfunc(putn(y, MONYY5.));
file=rec_&year.;
output;
end;
run;

but this time the %sysfunc() told me y is not a number.
Shmuel
Garnet | Level 18

@fsuzhang wrote:
Thank,
I need to convert it back to date, as below

data test2;
do k=0 to 10;
/*y= %sysfunc(intnx(month, &new_input., k , 'e'));*/
y=intnx('month', &new_input., k , 'e');
year=%sysfunc(putn(y, MONYY5.));
file=rec_&year.;
output;
end;
run;

but this time the %sysfunc() told me y is not a number.

You should open a new post, anyway - there is no need to use %sysfunc in a datastep

where you can use the sas function directly. 

Tom
Super User Tom
Super User

For a DATE literal, quoted string followed by the letter D, the string must be something the DATE informat can read.  You tried to use a value that did not include a value for the day of the month.  You were able to convert that to date using the MONYY informat because it assumes you want the first day of the month. 

 

You can mimic that if you want.

%let From_Month = JAN2007;
%let From_Date = "01&From_Month"d ;

PS Do NOT use only two digits for years. 

fsuzhang
Fluorite | Level 6

the saved files using 2 digit for year.

Tom
Super User Tom
Super User

I moved the messages added to other (somewhat) related posts into this thread.

In future if you want to ask a new question that is related to a question then make a new post.  You can include a link back to the old post to help explain your new question.

fsuzhang
Fluorite | Level 6

sorry, in a hurry, I posted to wrong thread.

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
  • 12 replies
  • 1007 views
  • 0 likes
  • 4 in conversation