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

Hi, i have use %macro variable so that i only need to enter the date i want everytime, However i was facing some issue in my code.

 

Fo Ex: for the MTH (month) that i pointed with an arrow in the code below, if i want year to be 2019 and the month is 02, the result get i want to be 01 (which is January) instead of only 1.

 

This is because, there is some pathway for me to read the data will have 201901 and not 20191

 

 

 

 

%macro date(YYYY=2019, MM=02,Y=2019,M=02);%global YR MTH ACCYRFY;

 

 

(body........)

 

%if &MM = 01 %then %do;

%let YR = %eval(&YYYY - 1);

%let MTH = 12;

%let ACCYRFY = &YYYY;

%end;

 

%else %if &MM=12 %then %do;

%let YR = &YYYY;

%let MTH = %eval(&MM-1);

%let ACCYRFY = %eval(&YYYY+1);

%end;

 

%else %do;

%let YR = &YYYY;

%let MTH = %eval(&MM-01); <-The result i get will be 1 instead of 01 if use this code.

%let ACCYRFY = &YYYY;

%end;

 

 

libname pl&YR&MTH. "C:\Data\POLA\&YR\&YR&MTH\DBF";

data pl&YR&MTH..pola_motor_clm_agg_&YR&MTH.;

set pl&YR&MTH..pola_motor_clm_agg_&YR&MTH.;

 

run;

libname pl&Y&M. "C:\Data\POLA\&Y\&Y&M\DBF";

data pl&Y&M..pola_motor_clm_agg_&Y&M.;

 

set

pl&YR&MTH..pola_motor_clm_agg_&YR&MTH.

pl&Y&M..pola_motor_clm_mth_&Y&M.;

 

 

%mend date;

%date(YYYY=2019,MM=02,Y=2019,M=02);

run;

 

 

how can i solve this problem?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
Simplest: combine the year and month ahead of time.

%let ym = %eval(100 * &yr + &mth);

If you must keep them separate, a few possibilities exist. I would lean toward:

%if &mth < 10 %then %let mth = 0&mth;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

You are trying to compute the previous month. That will be much easier to be done by a data step:

%macro date(YYYY=2019, MM=02,Y=2019,M=02);
  %global YR MTH ACCYRFY;
  
  data _NULL_;
       date1 = mdy(&mm, 01, &yyyy);
	   date2 = intnx('month', date1, -1);
	   call symput('datex',put(date2,yymmn6.));
	   /* datex replaces &yy&mth or you can define: */
	   call symput('yy', put(year(date2),4.));
	   call symput('mth',put(month(date2),z2.));
  run;
  
  ......
%mend date;

 

Kayla_Tan222
Calcite | Level 5

Sorry Shmuel, can you explain it more detail? coz actually I'm using the macro variable with some condition (I'm using if statement). it seems like unable to use your method is it is with the condition? I'm not so sure

 

%if &MM = 01 %then %do;

%let YR = %eval(&YYYY - 1);

%let MTH = 12;

%let ACCYRFY = &YYYY;

%end;

 

%else %if &MM=12 %then %do;

%let YR = &YYYY;

%let MTH = %eval(&MM-1);

%let ACCYRFY = %eval(&YYYY+1);

%end;

 

%else %do;

%let YR = &YYYY;

%let MTH = %eval(&MM-01); <-The result i get will be 1 instead of 01 if use this code.

%let ACCYRFY = &YYYY;

%end;

 

 

Shmuel
Garnet | Level 18

You are using conditions to check: 

- if current month is January then to compute previous moth you subtract 1 from year and assign month to 12

- otherwise you subtract 1 from the month.

 

The function MDY computes a sas date, which is numeric - counting the days from Jan 01, 1960  as zero.

 

The function INTNX computes a new date from a given date and time interval - in your case -1 month. There is

a third argument to define same day as in given date, last day or first day of the month. The default value is beginning of month,

that is 01. Anyway you don't use the day in your code. 

 

The function CALL SYMPUT assigns a value to a macro variable. Firs argument is the name of the macro variable

and the 2nd argument is its value.

 

The function put is used to convert the numeric date into yyyymm format (yymmn6. format).

 

I suggest you should read the documentation of those functions by searching google:

    " sas documentation <function xxxxxx>"

 

 

Astounding
PROC Star
Simplest: combine the year and month ahead of time.

%let ym = %eval(100 * &yr + &mth);

If you must keep them separate, a few possibilities exist. I would lean toward:

%if &mth < 10 %then %let mth = 0&mth;

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