Help using Base SAS procedures

Format in SAS

Not applicable
Posts: 0

Format in SAS

hello ,

I'm using the code :-

data period1;
infile 'c:\Cwa\period1_sales_data.dat.';
Input TransactionID $1-8
DateofSale $9-18
TimeofSale $19-26
@9 DateTimeofSale ANYDTDTM16.
LaptopModel $27-32
UnitsSold 33-35
Warranty 36;
format DateTimeofSale datetime21.

proc print data=period1;

I want to concatenate the varaibels DateofSale and TimeofSale.The new varaible in above code gives no entry.

Kindly guide,
Super Contributor
Super Contributor
Posts: 3,176

Re: Format in SAS

Posted in reply to deleted_user
You will want to first begin using SAS numeric variables for DATE, TIME and DATETIME strings. It can be done at INPUT time or with a SAS assignment statement, using the INPUT function and the appropriate INFORMAT.

Scott Barry
SBBWorks, Inc.

Again - recommended reading:

SAS Language Reference: Concepts, About SAS Date, Time, and Datetime Values

Recommended Google advanced search argument, this topic/post:

using date datetime variables
Posts: 9,365

Re: Format in SAS

Posted in reply to deleted_user

Is this the same data that you asked about in thess other postings?

Let's take a step back and consider this "fake" data:

In the above "fake" data:
-- the first variable occupies positions 1-8 on the data line and is a character string of 8 bytes
-- the second variable, a date, occupies positions 9-18 on the data line and is a date value of the form mmddyy which takes 10 bytes on the data line
-- the third variable, a time value occupies positions 19-26, and is a time value of the form hh:mm:ss, which takes 8 bytes on the data line

It is appropriate, as Scott explained, to read the second and third variables into numeric SAS variables, which means the use of an INFORMAT in the INPUT statement. Since these "fake" data rows are all "lined up" (meaning every data value starts in the same position). I would use COLUMN INPUT with an INFORMAT to read the second and third variables. I do not believe you can read from position 9-26 with any existing INFORMAT (such as ANYDTDTM) in order to make a DATETIME numeric variable, because there is no delimiter between the date value and the time value. The documentation for ANYDTDTM is here:

However, a SAS function can easily take numeric values for Date and Time and make a DATETIME variable. In fact, the documentation shows EXACTLY how to use a numeric date variable and a numeric time variable and bring them together to make a DATETIME variable using the DHMS function as documented here:

This program illustrates how you can write a test program in order to read the "fake" data values and verify that your INPUT statement is reading the input data correctly. The "DATALINES" section shows the "fake" data and INFILE DATALINES statement points to the bottom of the program as the place for SAS to read the data -- this program illustrates COLUMN INPUT with an INFORMAT to read DATE_VAR and TIME_VAR as numeric variables and then uses the DHMS function to create a DATE/TIME variable from your numeric variables. If you run the program, read the documentation and carefully examine the output, it might help you figure out how to fix your code and read your data correctly:
data testinput;
infile datalines;
Input @1 Char_var $8.
@9 Date_var mmddyy10.
@19 Time_var time8.;
dtvar = dhms(Date_var,0,0,Time_var);
format Date_var mmddyy10. Time_var time8. dtvar Datetime.;

proc print data=testinput;
title 'testinput dataset with formats applied';

For more information on how to read a numeric variable with an INFORMAT, refer to these documentation topics and papers, as well as the documentation recommended by Scott and others:

Scott already suggested that it would be better to read the 2 variables as numeric values and if you did this, you would not have to use the DATEPART and TIMEPART functions at all. Nor would you have to create DateTimeofSale by concatenating character strings and hoping that the concatenation would result in a numeric date/time value.

In several of your posts, you created the file PERIOD1 and in the program (posted with the error), you are creating the dataset RESULT from PERIOD1. There's no real reason to create RESULT in a separate DATA step program because it seems the only purpose of the RESULT file is to correct data problems that existed because of the issues noted in the creation of the PERIOD1 dataset.

It always helps if you post an example of your data (a few lines) and your WHOLE code and your LOG to give folks as much information as possible to help you. If you read the documentation, as suggested, and there's something you do not understand, then it would be better to ask about what you do not understand than to post code which makes it look like you are ignoring suggestions that have already been made.

For more information about how to post code (and perhaps a data sample to the forum), this previous posting talks about the use of [pre] and [/pre] to maintain code indenting and to post samples of data that will line up.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation