BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

User define a macro variable called YYMM (Represent Year(2 digits) and month), for example: 2306 represent year 2023 and month 06.

The target is to calculate 2 other macro variables:

Macro1-End of month date (in this example the macro variable will  get value '30JUN2023'd or 23191 because this number represent 30JUN2023)

Macro2-End of month date name without 'd ,in this example : 30JUN2023

(Note: I need it in order to add it to data set name )

What is the way to create macro1 and macro 2 via %let statement?

What is the way to create macro1 and macro 2 via  data set?

 

4 REPLIES 4
Patrick
Opal | Level 21
%let YYMM =2306;

%let mvar_2=%sysfunc(intnx(month,%sysfunc(inputn(&yymm,yymmn4.)),0,e),date9.);
%let mvar_1=%tslit(&mvar_2)d;

data _null_;
  sas_dt=intnx('month',input("&yymm",yymmn4.),0,'e');
  call symputx('mvar_1',cats("'",put(sas_dt,date9.),"'d"));
  call symputx('mvar_2',put(sas_dt,date9.));
run;
yabwon
Onyx | Level 15

Why not just:

%let mvar_1="&mvar_2."d;

?

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ballardw
Super User

I'm not going to do this with %let because given poorly structured stuff, YYMM with 2 digit years, as I believe was brought up to you multiple times when you first started asking questions on this forum, is just plain ugly and the number of %sysfunc nested calls that would be needed to to create this gets moderately hard just to follow the code.

 

This assumes all of your 2-digit years are 2000 or later. If not good luck and a reason not to use 2-digit years.

I am also including demonstration of YYYYMMNN as for file names that makes much better sense than date9 formated values. YYYYMMNN will sort correctly. Date9 won't even come close. 28Feb (OR 29Feb) then 30Jun (and all the  months that end on the 30th) then 31Apr. Garbage to find file names like that especially multiple years worth in the same folder.

 

%let yymm=2306;
data _null_;
   d =intnx('month',mdy(%substr(&yymm.,3,2),1,20%substr(&yymm.,1,2)),0,'E');
   call symputx('eom1',put(d,5. -l));
   call symputx('eom2',cats(quote(put(d,date9.)),'d'));
   call symputx('eom3',put(d,date9.));
   call symputx('eom4',put(d,yymmddn8.));
run;

%put eom1 = &eom1.  eom2= &eom2.  eom3= &eom3. eom4= &eom4.;

 

Kurt_Bremser
Super User

Do not use 2-digit years.

Do not use 2-digit years.

Do not use 2-digit years.

 

After Y2K, the use of 2-digit years constitutes the mark of an idiot (and I am being very polite here).

 

Never use something like the DATE9 format for time-stamping files or other objects. This does not sort chronologically and makes dealing with them harder. Always use dates in YMD order, either with the YYMMDDN8. (suited for SAS datasets) or the YYMMDD10. format.

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
  • 457 views
  • 5 likes
  • 5 in conversation