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

Greetings all.  I'm having some trouble creating a string variable in format 'YYYY-MM-DD' given an ending date.  I would like to supply an end date, then use symput (or whatever else works) to create the begin date.  Given dt_end = '2014-02-03', I would like dt_begin to be '2014-01-27', one week earlier, but it is ending up being '19750'.  Here is what I am trying...

%let dt_end = '2014-02-03' ;

data _null_ ;

    call symput('dt_begin', cat("'",input(&dt_end, yymmdd10.)-7,"'"));

run ; quit ;

%put &dt_begin ;

I will be passing these variables into an ODBC query, so I need the single quotes on them.  I can't understand why &dt_begin is in date serial format, and not YYYY-MM-DD format.  Does anyone have any ideas?  Thank you.

Greg

1 ACCEPTED SOLUTION

Accepted Solutions
DBailey
Lapis Lazuli | Level 10

%let dt_end = '2014-02-03' ;

data _null_ ;

     call symput('dt_begin', cat("'",put(input(&dt_end, yymmdd10.),yymmddd10.),"'"));

run ; quit ;

%put &dt_begin ;

View solution in original post

5 REPLIES 5
DBailey
Lapis Lazuli | Level 10

%let dt_end = '2014-02-03' ;

data _null_ ;

     call symput('dt_begin', cat("'",put(input(&dt_end, yymmdd10.),yymmddd10.),"'"));

run ; quit ;

%put &dt_begin ;

gsnidow
Obsidian | Level 7

DBailey, thank you so much.  If I'm telling the symput function that I want &dt_end to be in yymmdd10 format, why must I also tell it that I want the result to be in yymmdd10.?  That seems kind of redundant.  Anyhow, thanks again.

Greg

Tom
Super User Tom
Super User

The INPUT() function is a general tool for converting strings into values.  To know what type of conversion you need to provide it an INFORMAT so that it knows what type of conversion to perform.

Dates are stored as number of days since 1/1/1960.

The PUT() function is a general tool for converting a value into a string.  To know what type of conversion you need to provide it with a FORMAT.

gsnidow
Obsidian | Level 7

Thank you Tom.  I've been using SAS off and on for well over a year now, and I still find dates to be the most frustrating thing of all.

Greg

Peter_C
Rhodochrosite | Level 12

did you not notice you have to use the INPUT() function to convert your start date before subtracting 7 days?

Your result is a standard SAS-date (a number of days since 1-Jan-1960).

So you need to convert that with the PUT() function in a similar way.

Alternatively, work with SAS-date constants and convert to the DBMS format at the last stage (if you really need it even then)

SAS-date constants are a lot more understandable for us than tHat "number of days since 1960) Smiley Wink

There are hundreds of formats an informats to handle dates.There are also two really helpful functions for date arithmetic

  1. INTCK() calculates the number of date/time intervals between two dates (and/or times)
  2. INTNX() returns the next date/time after +/- N intervals

WEEK is one of those intervals although your question doesn't need more than a 7-day interval, i.e. old-date+/- 7

There are aspects to interval handling you might find very useful (although you dfon't need to go there for this issue)

Your date style is very similar to the style of Teradata dates so I guess you seek the constant to use in that (dbms) kind of context.

With implicit pass-thru SQL you can just use SAS-date constants, but generating a query for the DBMS environment I guess you might need that style for dates. Here is hou you could work

%let today = %sysfunc( today(), date9. ) ;

%let dt_end = %str(%')%sysfunc( putn( "&today"d, yymmddD10.))%str(%') ;

%let dt_begin = %str(%')%sysfunc( intnx( week, "&today"d,  -1 , same),yymmddD10.)%str(%') ;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 3657 views
  • 6 likes
  • 4 in conversation