Help using Base SAS procedures

Day format?

Accepted Solution Solved
Reply
Super Contributor
Posts: 297
Accepted Solution

Day format?

Hello,

 

I would like to assign the variable 'Daycreated' for the current day, 07/19/2017.  Also, I would like to change the format to 7/19/2017.   I have the code below.  Somehow, error messages were shown on the log.  Please advice how to fix it.  Thanks.

 

%let tday = %sysfunc(today(),yymmdd10.);

 

proc format library=work;

picture sddate

low-high = '%m/%d/%Y' (datatype=date);

run;

 

data test;

Daycreated ='&tday'd;

put Daycreated sddate.;

format Daycreated sddate.;

run;

 

61 %let tday = %sysfunc(today(),yymmdd10.);

62

63 proc format library=work;

64 picture sddate

65 low-high = '%m/%d/%Y' (datatype=date);

NOTE: Format SDDATE is already on the library WORK.FORMATS.

NOTE: Format SDDATE has been output.

66 run;

NOTE: PROCEDURE FORMAT used (Total process time):

real time 0.01 seconds

cpu time 0.01 seconds

 

67

68 data test;

69 Daycreated ='&tday'd;

--------

77

ERROR: Invalid date/time/datetime constant '&tday'd.

ERROR 77-185: Invalid number conversion on '&tday'd.

70 put Daycreated sddate.;

71 format Daycreated sddate.;

72 run;

NOTE: The SAS System stopped processing this step because of errors.

WARNING: The data set WORK.TEST may be incomplete. When this step was stopped there were 0

observations and 1 variables.

WARNING: Data set WORK.TEST was not replaced because this step was stopped.

 


Accepted Solutions
Solution
‎07-19-2017 05:14 PM
Super User
Posts: 5,085

Re: Day format?

The %LET statement should be removed entirely.  There is no need for a macro variable &TDAY.

 

The DATA step should simply assign a value to the DATA step variable:

 

Daycreated = today();

View solution in original post


All Replies
Solution
‎07-19-2017 05:14 PM
Super User
Posts: 5,085

Re: Day format?

The %LET statement should be removed entirely.  There is no need for a macro variable &TDAY.

 

The DATA step should simply assign a value to the DATA step variable:

 

Daycreated = today();

Super Contributor
Posts: 297

Re: Day format?

I got the result like,

 

Daycreated

7/19/201

 

Which is not I would like, How to get 7/19/2017?  Thanks.

Trusted Advisor
Posts: 1,383

Re: Day format?

[ Edited ]

The shortest code to achieve what you want is:

data test;
  datecreated = today();
  format datecreated mmddyy10.;
run;

besides, use macro variable with double quotes, not single quotes:

data test;
  Daycreated ="&tday"d;
run;

instead: 

    Daycreated ='&tday'd;

Super User
Posts: 5,085

Re: Day format?

The format width is not wide enough.  You may need 10 characters, so should specify:

 

put Daycreated sddate10.;

format Daycreated sddate10.;

 

You can experiment with forcing the result to be left-hand justified, such as:

 

put Daycreated sddate10.-l;

Super Contributor
Posts: 297

Re: Day format?

[ Edited ]

So, I changed the codes based on your suggestion.  However, I found there is a problem.  I ran an extra program to change all of numeric to charterer in my actual dataset.  It turned out the 'Daycreated' became 21019.  Before running the macro, the format was perfectly fine.   Why this happened?  How to fix it?  Basically, I don't want to change my macro program codes.  Thanks.

 

proc format library=work;

picture sddate

low-high = '%m/%d/%Y' (datatype=date);

run;

data test;

Daycreated = today();

put Daycreated sddate10.;

format Daycreated sddate10.;

run;

%macro vars(dsn);

%mend vars;

%vars (test)

 

Super User
Posts: 5,085

Re: Day format?

21109 is today's date on SAS's date scale.  It's the actual number returned by the TODAY() function.

 

Then your macro goes to work on all the data values, converts them ...

 

It looks like a side effect of the macro is to remove the format that was previously applied by the FORMAT statement.  If that is what is happening here, you will need to re-issue the format in a DATA step to override the changes introduced by your macro:

 

format DayCreated sddate10.;

 

If you created a character version of _DayCreated, you might want to replace that as well since your macro uses the 8. format for all conversions.  If you wish to do that, you will need to DROP the character variable first, because its length is 8 not 10, and then add to your DATA step:

 

_DayCreated = put(DayCreated, sddate10.);

 

Super Contributor
Posts: 297

Re: Day format?

It works, you are awesome!  Thank you so much for your wonderful help, Astounding. 

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 365 views
  • 1 like
  • 3 in conversation