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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
Astounding
PROC Star

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();

ybz12003
Rhodochrosite | Level 12

I got the result like,

 

Daycreated

7/19/201

 

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

Shmuel
Garnet | Level 18

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;

Astounding
PROC Star

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;

ybz12003
Rhodochrosite | Level 12

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)

 

Astounding
PROC Star

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.);

 

ybz12003
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 1128 views
  • 1 like
  • 3 in conversation