Help using Base SAS procedures

Format in SAS

Reply
N/A
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;
run;

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

Kindly guide,
markc
Super Contributor
Super Contributor
Posts: 3,174

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
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a002200738.htm

Recommended Google advanced search argument, this topic/post:

using date datetime variables site:sas.com
SAS Super FREQ
Posts: 8,862

Re: Format in SAS

Posted in reply to deleted_user
Hi:

Is this the same data that you asked about in thess other postings?
http://support.sas.com/forums/thread.jspa?threadID=8816&tstart=0
http://support.sas.com/forums/thread.jspa?threadID=8817&tstart=0
http://support.sas.com/forums/thread.jspa?threadID=8818&tstart=0
http://support.sas.com/forums/thread.jspa?threadID=8715&tstart=30

Let's take a step back and consider this "fake" data:
[pre]
|------||--------||------|
aaaaaaaa01/01/201001:15:36
bbbbbbbb11/15/195010:23:15
cccccccc08/23/199921:17:58
[/pre]

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:
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a002605552.htm

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:
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000179419.htm

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:
[pre]
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.;
return;
datalines;
aaaaaaaa01/01/201001:15:36
bbbbbbbb11/15/195010:23:15
cccccccc08/23/199921:17:58
;
run;

proc print data=testinput;
title 'testinput dataset with formats applied';
run;
[/pre]

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:
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001360509.htm
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000144370.htm
http://support.sas.com/techsup/technote/ts668.pdf
http://www2.sas.com/proceedings/sugi30/255-30.pdf
http://www2.sas.com/proceedings/sugi29/253-29.pdf

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.
http://support.sas.com/forums/thread.jspa?threadID=7550

cynthia
Ask a Question
Discussion stats
  • 2 replies
  • 152 views
  • 0 likes
  • 3 in conversation