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

Appreciate if someone of you help me understand to create the variable 'SDDTOP' and it should have the date value (last day of previous month)  with character format. 

 

e.g.  SDDTOP=2018-03-31

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@Babloo wrote:
What if the value of 'have' is 2017-03-31 in numeric format and I just to
need to convert it to character? Can I use the put function with the
format $10.?

@Babloo

I would have thought you understand in-between how SAS Date and DateTime values work. If the value is numeric then it's either the count of days (date value) or seconds (datetime value) since 1/1/1960. You then apply a format to make this value human readable as a date/datetime value. This doesn't change the value itself which just remains a number, the format is just for printing.

If you really want to store such a numeric value representing a data/datetime in a character variable, then you have to use the put() function. This function does nothing else than applying the format you use to your numeric value and then write the result as a string - and if this is on the right side from the equal sign then you're assigning this string value to a character variable. In other languages the comparable function to a SAS put() is called write()  - and read() for input().

 

Can I use the put function with the format $10.?

I hope you understand now that the answer to this question is "no, of course not". You have to use the appropriate date/datetime format to write the number as a date/datetime string.

 

And last but not least: It's really MUCH better to keep dates as SAS date/datetime values (=numeric) with a format permanently assigned to the variable for printing/looking at it. Keeping the dates numeric allows you to actually use such data (i.e. if you want to calculated the number of days between two dates and the like; or even if you just want to sort by date). 

View solution in original post

12 REPLIES 12
Babloo
Rhodochrosite | Level 12

I already tried this, but it is in numeric format. I want it to be in character format.

Babloo
Rhodochrosite | Level 12

I tried this code and end up with the invalid values like ******** for the variable b and c.

 

 

data test;
a='31JAN2017:00:00:00'dt;
format a datetime23.;
b=PUT(a,date9.);
c=put(a,YYMMDD10.);
run;

My objective is to create two new variable based on the variable 'a'. One variable (e.g. 'b') should have value the 31JAN2017 in numeric format and another one (e.g. 'c') should have the value 2017-01-31 and it should be in character format.

 

Kurt_Bremser
Super User

So you do not have a DATE value, as you told lyingly to us, but a DATETIME value. As you were told probably a hundred times now, a date value is a count of DAYS, while a datetime value is a count of SECONDS, so a datetime value is MUCH too big for a date format. There is a magic function called DATEPART that extracts the date out of a datetime. USE IT.

Babloo
Rhodochrosite | Level 12

I managed to create one of the two variables using datepart function. However I'm seeking your help to create the variable and value as below (last day of previous month) and it should be in character format.  Challenge which i have here is to make this as a character variable.

 

SDDTOP = 2018-03-31

RW9
Diamond | Level 26 RW9
Diamond | Level 26
data test;
  a='31JAN2017:00:00:00'dt;
  format a datetime23.;
  b=put(datepart(a),date9.);
  c=put(datepart(a),yymmdd10.);
run;
Patrick
Opal | Level 21

@Babloo wrote:

I managed to create one of the two variables using datepart function. However I'm seeking your help to create the variable and value as below (last day of previous month) and it should be in character format.  Challenge which i have here is to make this as a character variable.

 

SDDTOP = 2018-03-31


@Babloo

The way I went about this: 

1. Google search:   site:support.sas.com 9.4 formats and informats

That returns as the very first hit: https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm 

2. Go to the documentation for Formats

3. Open up "Formats by category"

Capture.JPG

4. search for yyyy-   

....and this then led me to format e8601dn. which returns exactly what you're asking for without even the need to first convert your SAS datetime to a date value.

data test;
  have='31JAN2017:00:00:00'dt;
  format have datetime23.;
  want=put(have,e8601dn.);
run;

Capture.JPG

 

Babloo
Rhodochrosite | Level 12
What if the value of 'have' is 2017-03-31 in numeric format and I just to
need to convert it to character? Can I use the put function with the
format $10.?
Patrick
Opal | Level 21

@Babloo wrote:
What if the value of 'have' is 2017-03-31 in numeric format and I just to
need to convert it to character? Can I use the put function with the
format $10.?

@Babloo

I would have thought you understand in-between how SAS Date and DateTime values work. If the value is numeric then it's either the count of days (date value) or seconds (datetime value) since 1/1/1960. You then apply a format to make this value human readable as a date/datetime value. This doesn't change the value itself which just remains a number, the format is just for printing.

If you really want to store such a numeric value representing a data/datetime in a character variable, then you have to use the put() function. This function does nothing else than applying the format you use to your numeric value and then write the result as a string - and if this is on the right side from the equal sign then you're assigning this string value to a character variable. In other languages the comparable function to a SAS put() is called write()  - and read() for input().

 

Can I use the put function with the format $10.?

I hope you understand now that the answer to this question is "no, of course not". You have to use the appropriate date/datetime format to write the number as a date/datetime string.

 

And last but not least: It's really MUCH better to keep dates as SAS date/datetime values (=numeric) with a format permanently assigned to the variable for printing/looking at it. Keeping the dates numeric allows you to actually use such data (i.e. if you want to calculated the number of days between two dates and the like; or even if you just want to sort by date). 

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!

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
  • 12 replies
  • 7045 views
  • 3 likes
  • 4 in conversation