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.

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
  • 755 views
  • 3 likes
  • 4 in conversation