BookmarkSubscribeRSS Feed
mrdlau
Obsidian | Level 7

I've been reading everywhere but I'm still I'm thoroughly confused about puts/inputs and formats/informats and just the concept of converting back and forth.

 

Am I right about this?

Input is used when I enter raw data and give it a format.  i,e  I create a variable 1, and I declare it to be a numeric.

Put  is used when I want to actually display a format to look differently.  i,e Date to Date, i.e  convert "09Dec2016 00:00:00" to "12-9-2016."

 

informat goes with input, format goes with put.

 

Suppose, I have a value of "09Dec2016 00:00:00".  the current format is date.  when I used the put function with argument YYmmdd8, I got a date range out of bounds."  what did this happen?

 

Basically,  my end goal is to convert the above date to a numeric format of YYYYMMDD, but I dont know where to start.  I can google the answer online and some of them use a combo of input and put together, but I do not understand the why or how.

3 REPLIES 3
PGStats
Opal | Level 21

You are missing a crucial piece of the puzzle. SAS has two ways to represent dates: SAS dates and SAS datetimes. Both are just numbers but SAS dates are expressed in days and SAS datetimes are expressed in seconds. Formats and informats are associated with one or the other, but never both. If you use a date format on a datetime value or a datetime format on a date value, you get either a meaningless display or an error.

 

To convert a SAS datetime that prints as "09Dec2016 00:00:00" to a SAS date that can be displayed as "12-09-2016", use the function DATEPART(). To go from date to datetime, use function DHMS().

 

data _null_;
dt = '09Dec2016 00:00:00'dt;
d = datepart(dt);
put dt= datetime16. d= mmddyy10.;
run;
PG
Kurt_Bremser
Super User

put() and input() are conversion functions. The result of input() can be character or numeric, while the result of put() is always character.

eg

x1 = '41';
x2 = input(x1,$hex2.);
x3 = input(x1,hex2.);

will store a value of 'A' in character variable x2, and a value of 65 in numeric variable x3.

 

The input and put statements are use to read and write data in text form from/to external data sources; if no output file is specified, put will write to the log.

 

As you correctly observed, the input statement and function uses informats, while put uses formats.

 

Your problem happens because of an improper value which cannot be converted by the date format.

Dates and datetimes are not variable types per se; both are just numeric, and have a date or datetime format attached.

Since dates are numeric values counting the days from 01jan1960, while datetimes count seconds from 01jan1960:00:00:00, datetimes usually have values that make no sense as days.

 

To extract the date part from a datetime value, use the datepart() function, and assign the newly created variable a date format.

Or create a character variable to hold the formatted value:

length newvar_numeric 5 newvar_char $8;
format newvar_numeric yymmddn8.;
newvar_numeric = datepart(oldvar);
put newvar_numeric=;
newvar_char = put(datepart(oldvar),yymmddn8.);
put newvar_char=;
Patrick
Opal | Level 21

Trying to say it simple:

- INPUT reads a string into a SAS variable. The INFORMAT instructs SAS how to read this string and convert it as a value of a SAS variable.

- PUT WRITES a string to some output destination using a SAS variable. The FORMAT instructs SAS how to convert the SAS variable value and write the string.

 

...and as others already wrote: SAS stores date as numbers but has two ways of doing so; either as number representing a date (days since 1/1/1960) or as number representing a datetime (seconds since 1/1/1960).

 

Sooo... if you have a SAS numeric variable whose value stands for todays SAS datetime but you apply a format for dates then you'd end up in a date so far in the future that SAS doesn't even "bother" anymore to write this date.

The other way round if you have a SAS numeric variable whose value stands for todays date and you apply a datetime format then you'd see some datetime value of 1960 (right now I'm getting 01JAN60:05:44:42).

 

And now that you understand how SAS date and datetime values work: You can convert them. There is a datepart() and timepart() function which allows you to extract the date or time part from a SAS datetime value. You could do this also on your own.

Datetime is in seconds, date is in days - so simply divide the datetime value through the seconds of a day (86400) and take the integer of that. This is the corresponding SAS date value (and what the datepart() function does for you). Same goes with the timepart() function - it's the fractional bit after the comma once you've devided through 86400. 

 

Hope that didn't confuse more than it helped.

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 5110 views
  • 8 likes
  • 4 in conversation