DATA Step, Macro, Functions and more

Using Let statements conditionally

Reply
New Contributor
Posts: 2

Using Let statements conditionally

Hi I'm having trouble getting the answer to a very trivial problem.

 

I'm trying to get my previous date (year and month) to account for the flip of the year scenario.

 

Example:

yyyymm = 201805

pyyyymm = 201804

 

yyyymm = 201801

pyyyymm = 201712

 

But the following program just spews out 201800 for when yyyymm = 201801...and i cant figure out why?

Thanks.

 

%let yyyymm = 201801;

 

%let yr = %sysfunc(substr(&yyyymm,1,4));
%let mth = %sysfunc(substr(&yyyymm,5,2));

 

data null_;
if "&mth" = '01' then do;
%let pyyyymm = cats(&yr.-1, 12);
end;
else do;
%let pyyyymm = &yyyymm.-1;
end;
run;

Super User
Posts: 2,046

Re: Using Let statements conditionally

[ Edited ]

Are you looking to do something like this?

%macro waldo11;
 

%let yyyymm = 201801;

 

%let yr = %sysfunc(substr(&yyyymm,1,4));
%let mth = %sysfunc(substr(&yyyymm,5,2));

 

%if &mth = 01 %then %do;
%let pyyyymm = %sysfunc(cats(&yr.-1, 12));
%end;
%else %do;
%let pyyyymm = %val(&yyyymm.-1);
%end;

%put pyyyymm=&pyyyymm;
%mend waldo11;

%waldo11

/*or*/


%let yyyymm = 201801;



%let yr = %sysfunc(substr(&yyyymm,1,4));
%let mth = %sysfunc(substr(&yyyymm,5,2));
data _null_;
if "&mth" = '01' then do;
call symput( 'pyyyymm',cats(&yr.-1, 12));
end;
else do;
call symput('pyyyymm',&yyyymm.-1);
end;
run;

%put &=pyyyymm;
Super User
Super User
Posts: 8,264

Re: Using Let statements conditionally

[ Edited ]

Macro code is used to generate SAS code. By the time the SAS code starts running the macro code has been run already.

So you are running these commands.

%let yyyymm = 201801;
%let yr = %sysfunc(substr(&yyyymm,1,4));
%let mth = %sysfunc(substr(&yyyymm,5,2));
%let pyyyymm = cats(&yr.-1, 12);
%let pyyyymm = &yyyymm.-1;

data null_;
  if "&mth" = '01' then do; end;
  else do; end;
run;

If you want to use your data step to generate macro variables then use the CALL SYMPUTX() function.

data null_;
  if "&mth" = '01' then call symputx('pyyyymm',cats(&yr.-1, 12));
  else call symputx('pyyyymm',&yyyymm.-1);
run;

Of course if you are just trying to subtract one month from a date then convert your strings into dates and use functions.

%let pyyyymm=%sysfunc(intnx(month,%sysfunc(inputn(&yyyymm.01,yymmdd8)),-1),yymmn6);
Respected Advisor
Posts: 3,251

Re: Using Let statements conditionally

Definitely, as @Tom said, you should be using SAS dates to do the calculations and the INTNX function to do this. 

 

Don't try to do math on human readable dates.

 

 If you need to convert the results to a human readable format (which SAS dates are not), then when you do the output, apply the proper format.

--
Paige Miller
SAS Super FREQ
Posts: 9,423

Re: Using Let statements conditionally

Hi:
You are mixing DATA step logic with creating a Macro variable. If you want to create a macro variable conditionally, then your choices are
1) to write a macro program definition and use macro logic with %IF or
2) to write a DATA step program and use CALL SYMPUT or CALL SYMPUTX.

When you use a standard DATA step program the %LET is resolved at compile time, not at execution time, so the results will not really execute conditionally, as you envision because the %let statements are not really part of the program anymore, after compile time.

Cynthia
New Contributor
Posts: 2

Re: Using Let statements conditionally

Posted in reply to Cynthia_sas
A lot more clear now - thanks everyone!
Ask a Question
Discussion stats
  • 5 replies
  • 122 views
  • 6 likes
  • 5 in conversation