Help using Base SAS procedures

How to replace a string in global variable?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

How to replace a string in global variable?

I would  like to use the export procedure in order to create an excel with a time stamp, but proc export does not like the :.

So I define the global variable

%let zeitstempel = %SYSFUNC(DATE(), DDMMYYP10._at_%SYSFUNC(TIME(),TIME5.);

%put &zeitstempel.;

This works fine.

Now I try to replace the : by _

%let zs = %SYSFUNC(tranwrd(&zeitstempel.),":","_");

%put &zs.;

                    

This gets ignored by sas.

Do I need to add quotation marks and get rid of them later?

Is there another way to achiveIs this?

Is there maybe another time format that I can set in order to avoid my problem?

Thanks

Gregor



Accepted Solutions
Solution
‎03-09-2015 07:37 AM
Esteemed Advisor
Esteemed Advisor
Posts: 6,996

Re: How to replace a string in global variable?

Hi,

I would recommend against having dates/times in filenames.  For two reasons, time/date is not an easy thing to work with, and version control systems generally need the filename to stay the same. 

If you continue though, the below will switch : for _:

data _null_;

  call symput('DT',cats(put(date(),ddmmyyp10.),"_at_",tranwrd(put(time(),time5.),":","_")));

run;

%put &DT.;

Personally I wouldn't try to do this in %let statements.  If you have to deal with parameters then put them in a datastep and call symput/execute the required output.

View solution in original post


All Replies
Solution
‎03-09-2015 07:37 AM
Esteemed Advisor
Esteemed Advisor
Posts: 6,996

Re: How to replace a string in global variable?

Hi,

I would recommend against having dates/times in filenames.  For two reasons, time/date is not an easy thing to work with, and version control systems generally need the filename to stay the same. 

If you continue though, the below will switch : for _:

data _null_;

  call symput('DT',cats(put(date(),ddmmyyp10.),"_at_",tranwrd(put(time(),time5.),":","_")));

run;

%put &DT.;

Personally I wouldn't try to do this in %let statements.  If you have to deal with parameters then put them in a datastep and call symput/execute the required output.

Occasional Contributor
Posts: 19

Re: How to replace a string in global variable?

This works perfectly.  I need to export excels with time stamps for documentary reasons. The numbers I calculate here have an impact on the data I deliver to an external recipant.

Thanks a bunch.


Super User
Super User
Posts: 6,153

Re: How to replace a string in global variable?

The problem is the quotes. In macro code everything is a character strings so you do not need to put quotes around strings to distinguish them from numbers or variable names.

%let zs = %SYSFUNC(tranwrd(&zeitstempel.),:,_));

Message was edited by: Tom Abernathy Fixed missing right parenthesis.

Occasional Contributor
Posts: 19

Re: How to replace a string in global variable?

This leads to ERROR: Expect close parenthesis after macro function not found.

Thanks a lot, anyway.


Super User
Super User
Posts: 6,153

Re: How to replace a string in global variable?

Add the extra ) required because TRANWRD() function is wrapped inside of %SYSFUNC() call.

Grand Advisor
Posts: 9,463

Re: How to replace a string in global variable?

customize  date format , you can get any form of date.

proc format;

picture fmt

low-high='%0d.%0m.%Y_at_%0H_%0M' (datatype=datetime);

run;

%let zeitstempel = %SYSFUNC(DATETIME(), fmt.);

%put &zeitstempel.;

Xia Keshan

Grand Advisor
Posts: 9,463

Re: How to replace a string in global variable?

customize  date format , you can get any form of date.

proc format;

picture fmt

low-high='%0d.%0m.%Y_at_%0H_%0M' (datatype=datetime);

run;

%let zeitstempel = %SYSFUNC(DATETIME(), fmt.);

%put &zeitstempel.;

Xia Keshan

Contributor
Posts: 24

Re: How to replace a string in global variable?

/*The above step won't work becoz you didnt mask the quotes..So see below*/

%let zs = %SYSFUNC(tranwrd(&zeitstempel.,%str(Smiley Happy,%str(_)));

%put &zs.; 

Super User
Super User
Posts: 6,153

Re: How to replace a string in global variable?

The problem is not that the quotes are not masked, but that the quotes are included in the string to find and so there is no match.

Also note that neither : or _ are macro triggers so there is no need to use %STR() to quote them.

Contributor
Posts: 24

Re: How to replace a string in global variable?

Yeah. Exactly... Thanks Tom.

Simply we can write as below,

%let zs = %SYSFUNC(tranwrd(&zeitstempel.:,_));

%put &zs.;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 266 views
  • 8 likes
  • 5 in conversation