SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
WaiLoon
Calcite | Level 5

Equipment Data
Voltage
Master Project
AIPM workflow

 


Hoza by BA level
User access by State/Hoza/BA

GIS Data

 

 

Hi all

My program getting hardware list from another system, but the system don't have CommissionDate and DecommissionDate. Source system is in Excel and Target is PostgreSQL. 

Therefore I need to generate these 2 value every month when ETL the hardware list.

So, what I planned to do is, if the hardware from source DB not in the target DB, then I will insert and set the CommissionDate to 1st of current month.

If the hardware in target DB not in source DB, then I will set the DecommissionDate to last day of previous month.

I tried many way but unfortunately can't get it work. I can't even get the previous month work.


But today I tried this
%let Decomm=%sysfunc(intnx(month,%sysfunc(today()),-1,same), yymmdd8.);
%put &Decomm;

And I get
276 %let Decomm=%sysfunc(intnx(month,%sysfunc(today()),-1,same), yymmdd8.);
277 %put &Decomm;
SYMBOLGEN: Macro variable DECOMM resolves to 22-08-13
22-08-13

it seems like is I manage to get previous month, but I need the day to be last day of the month and also the format should be dd/mm/yyyy.

 

Today I tried this syntax

%let Decomm=%sysfunc(intnx(month,%sysfunc(today()),-1,same), ddmmyyyy8.);
%put &Decomm;

 

and I get this

276 %let Decomm=%sysfunc(intnx(month,%sysfunc(today()),-1,same), ddmmyyyy8.);
277 %put &Decomm;
SYMBOLGEN: Macro variable DECOMM resolves to 22870
22870

 


Other than above, i also need to get the first day of current month...

May I know what is my script missed??

1 ACCEPTED SOLUTION

Accepted Solutions
10 REPLIES 10
PaigeMiller
Diamond | Level 26

INTNX has several choices for the final option. Instead of same, you can use end or beginning. That will get you the desired end of month date or beginning of month date.

 

Today I tried this syntax

%let Decomm=%sysfunc(intnx(month,%sysfunc(today()),-1,same), ddmmyyyy8.);
%put &Decomm;

 

and I get this

276 %let Decomm=%sysfunc(intnx(month,%sysfunc(today()),-1,same), ddmmyyyy8.);
277 %put &Decomm;
SYMBOLGEN: Macro variable DECOMM resolves to 22870
22870

 

You should see an ERROR in the log

 

ERROR: Format name DDMMYYYY8. not found or the width and/or decimal specified for the format used are out of range.

 

Don't ignore errors like this. It is telling you important information. It is telling you that you are trying to use a format name that doesn't exist. The format you should use is DDMMYY8.

--
Paige Miller
WaiLoon
Calcite | Level 5
Thanks. But what if I need to have yyyy, how should I format??
PaigeMiller
Diamond | Level 26

@WaiLoon wrote:
Thanks. But what if I need to have yyyy, how should I format??

Please try DDMMYY10. it will give you a four digit year.

--
Paige Miller
Kurt_Bremser
Super User

Use INTNX with correct parameters:

%let first_day_curr_month = %sysfunc(intnx(month,%sysfunc(today()),0,b));
%let last_day_prev_month = %eval(&first_day_curr_month. - 1);

If you need the values in assignments and calculations, don't bother with formatting them, see Maxim 28.

WaiLoon
Calcite | Level 5

Hi, thanks for suggestions, and sorry for the late reply cause suddenly got some production issue need to resolve. 

 

I tried the suggested syntax but still not able to get correct date.

 

%let first_day_curr_month = %sysfunc(intnx(month,%sysfunc(today()),0,b));
%let last_day_prev_month = %eval(&first_day_curr_month. -1);

UPDATE HardwareList tgt
SET DateDecommissioned = &last_day_prev_month

WHERE NOT EXISTS (.......);

 

I check the data, the DateDecommissioned  is 1960-01-01 06:21:28

Anything i did wrong? 

 

Patrick
Opal | Level 21

@WaiLoon Not that I see that anywhere in the code you've shared BUT... SAS got DATE and DATETIME values. 

A SAS Date value is the count of days since 1/1/1960, a SAS DateTime value is the count of seconds since 1/1/1960.

 

Most of the time if you get something like DateDecommissioned  is 1960-01-01 06:21:28 it's an indication that you've used a SAS DateTime FORMAT to print a SAS Date value (=prints your count of days as if it were a count of seconds).

Kurt_Bremser
Super User
So your target variable is datetime, not a date.
Change the assignment:

%let first_day_curr_month = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),0,b));
%let last_day_prev_month = %eval(&first_day_curr_month. -1);
WaiLoon
Calcite | Level 5

I finally get it works, so date and datetime having different handling... it is very different from the normal SQL... 

Reeza
Super User

@WaiLoon wrote:

I finally get it works, so date and datetime having different handling... it is very different from the normal SQL... 


Normal SQL typically only has datetime fields.

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

Kurt_Bremser
Super User

There is no "normal" SQL. Different database systems handle dates and times differently.

For instance, IBM's DB/2 treats them exactly like SAS, as counts of days and counts of seconds, respectively.

(This allows to store dates in 4 bytes as default)

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 10 replies
  • 3945 views
  • 7 likes
  • 5 in conversation