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

Hello,

 

I using a pass through connection to connect to an oracle Table as follow:

 

%macro CXNOracle(user=, password=, path=, schema=);

 

proc sql ;

CONNECT to oracle as oradb

(user=&user. password=&password. path=&path. );

create table &GFileName. as

 

SELECT *

FROM connection to oradb

(SELECT *

FROM &schema..&GFileName.) ;

 

DISCONNECT from oradb;

quit;

run;

%Mend CXNOracle;

 

%CXNOracle(user=*****, password=****, path=*****, schema=****);

 

As you can see, I input my user name, password, the place where is the oracle table then a schema.

The FileName is defined as a global variable as GFileName.

 

I have let's six date variable with the datetime20 format.

 

The challenge is to convert all date variables from a datetime20 format to YYYYMMDD without naming the variable.

Is there a way to set all date variables to a default format ex: YYYYMMDD?

 

Date variable as the following format = 31JAN1966:00:00:00

 

Thanks in advance for your help.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

There is no silver bullet for this. You need to identify the columns (eg by looking for certain attributes in dictionary.columns) and then apply the suggested method in dynamically created code, if you want to automate this conversion.

View solution in original post

6 REPLIES 6
alepage
Barite | Level 11

Hello Kurt,

 

As I have mentioned, I calling the oracle table only by their name.  Some table may have two date variables other six date variables and so on.

 

What I am looking for is a way to convert all date variables to YYYYMMDD in one shot without having to format all date variable one by one, like a default format for date variable.

 

Does your code will do that?

 

 

Shmuel
Garnet | Level 18

Please check, either by proc contents or by sql, selecting dictionary.columns - do the datetime variables have a format of datetime20. as you mentioned.

 

If positive, it is possible to identify those variables through their format, convert then to date variables and reformat them,

to be done either by data step or by a macro, without renaming them.

 

 

Kurt_Bremser
Super User

There is no silver bullet for this. You need to identify the columns (eg by looking for certain attributes in dictionary.columns) and then apply the suggested method in dynamically created code, if you want to automate this conversion.

abqdiane
Obsidian | Level 7
Wonderful - Thanks
Tom
Super User Tom
Super User

Use the FMTINFO() function to find the DATETIME variables. 

You could use something similar to the answer to this question. 

https://communities.sas.com/t5/SAS-Procedures/Change-DATE-formats-to-DDMMYYS10-for-ALL-unknown-numbe...

%macro CXNOracle(user=, password=, path=, schema=);
%if %symexist(gfilename) %then %do;
  %if %length(&gfilename) %then %do;  

libname oradb oracle user=&user password=&password path=&path schema=&schema access=readonly ;
proc contents data=oradb.&gfilename noprint out=_contents; run;
filename oracode temp;
data _null_;
  if 0=nobs then put "ERROR: No variables found for &gfilename..";
  file oracode ;
  set _contents obs=nobs end=eof;
  if _n_=1 then put
      "date &gfilename(label='From &path..&schema..&gfilename')"
    / " set oradb." &gfilename ';'
  ;
  if fmtinfo(format,'cat')='datetime' then put
      name '=datepart(' name ');'
    / 'format ' name 'yymmdd10.;'
  ;
  if eof then put 'run;' ;
run;
%include oracode / source2 ;

%else %put ERROR: Macro variable GFILENAME is empty.;
%else %put ERROR: Macro variable GFILENAME does not exist.;
libname oradb clear ;
%mend CXNOracle;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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