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

Howdy,

I'm new to programming and trying to figure out the best way to do the following:

I have a macro variable "Start_Date" which will be changed by users:

%let Start_Date = '06FEB2013:0:0:0'dt;

I'd like to take the year of the "Start_Date" value and find the first day of the year before.

The desired output using the date above would be '01JAN2012:0:0:0'dt;

Conceptually I loosely know of two routes to take....

Route 1: I could turn my date into a string, grab the "2013" off my string, create a string resembling a date, then turn this string into a date, arriving at the desired first day of the year.

Something like:

Date = 2/06/2013

=String(2/06/2013)

output: 20130206

=Left(20130206,1,4)

output: 2013

=1&"/"&1&"/"&2013

output: 1/1/2013

=Datevalue(1/1/2013)

Output: 1/1/2013 in date format or 1/1/2012 if subtracting one from the current year.

Route 2: use a SAS function for date shift to calculate at the correct date. In searching the forums I've seen intnx and mdy used in what looks like a similar goal.

Would anyone have an efficient route to pursue?

TS

1 ACCEPTED SOLUTION

Accepted Solutions
CTorres
Quartz | Level 8

I agree with you: second method will do the work.

I suggest the following improvement to get the result in datetime format:

%let Start_Date = '06FEB2013:0:0:0'dt;

data _null_;
prevybeg=dhms(intnx('year',datepart(&Start_Date),-1,'b'),0,0,0);
format prevybeg datetime19.;
put prevybeg=;
run;

CTorres

View solution in original post

4 REPLIES 4
Loko
Barite | Level 11

Hello,

Second method will do it.

%let Start_Date = '06FEB2013:0:0:0'dt;

data _null_;
prevybeg=intnx('year',datepart(&Start_Date),-1,'b');
format prevybeg date9.;
put prevybeg=;
run;

CTorres
Quartz | Level 8

I agree with you: second method will do the work.

I suggest the following improvement to get the result in datetime format:

%let Start_Date = '06FEB2013:0:0:0'dt;

data _null_;
prevybeg=dhms(intnx('year',datepart(&Start_Date),-1,'b'),0,0,0);
format prevybeg datetime19.;
put prevybeg=;
run;

CTorres

GalacticAbacus
Obsidian | Level 7

Thank you, intnx worked well. Was an excellent example.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Personally I don't like macro variables, I tend to store any parameters in datasets and then use them from there.  For instance an example.  I have some records and I want to flag records with a dt after the default then you could just left join that dataset onto your working data and use date functions.  Far easier than messing around with character macro variables.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1413 views
  • 3 likes
  • 4 in conversation