BookmarkSubscribeRSS Feed
Godzilla_Hat
Obsidian | Level 7

Elvis Presley was born on January 8, 1935 and died August 16, 1977.

   Using DATALINES in a SAS data step, read in his birth date as 08JAN1935 and the date of his death as 08-16-1977. Use a calculation in your data step to determine the number of days Elvis lived. Print out both dates as SAS dates with the number of days Elvis lived.

How would this code look and how would I write it out. I have never used SAS like this before. 

3 REPLIES 3
Godzilla_Hat
Obsidian | Level 7
data elvis;
infile cards expandtabs;
input bdate ddate dayslived;
datalines; 
08JAN1935 
08-16-1977
;
run; 

This is what I have so far in the program but unsure of where I'm suppose to go after.

ballardw
Super User

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

 

 

One of the functions, INTCK is specifically designed to get number of intervals between two date values.

 

When reading data generally you want all the values on your input statement on a single line (exceptions are not for real beginners).

You need to provide and INFORMAT to tell SAS how to read the data values. This could be done with either an INFORMAT statement or modifiers on an input statement.

 

To display a value as a date assign a FORMAT that will show the date in the format you want. SAS supplies about 40 date formats and the Proc Format will let you make just about any practical (and few downright weird) date displays.

Since you are expected to calculate the number of days do not attempt to read a value (that you do not have).

 

Very large economy sized hint: Read your log. SAS is actually pretty good about providing usable information in the log about things that may not happen correctly. For example running your data step:

 

109  data elvis;
110  infile cards expandtabs;
111  input bdate ddate dayslived;
112  datalines;

NOTE: Invalid data for bdate in line 113 1-9.
NOTE: Invalid data for ddate in line 114 1-10.
NOTE: LOST CARD.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+--
115        ;
NOTE: Invalid data errors for file CARDS occurred outside the printed range.
NOTE: Increase available buffer lines with the INFILE n= option.
bdate=. ddate=. dayslived=. _ERROR_=1 _N_=1
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
NOTE: The data set WORK.ELVIS has 0 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

How to read this: The Invalid data comes from not providing an informat to read the dates. If you do not provide additional information SAS will assume the variable is numeric and read it with a generic informat called W.D where W is the maximum number of characters to read (ignore the .D for informats for now, it will only cause problems and is mostly related to reading old punch card data). However the value can't contain anything other than digits, decimal points or the letter E for values written in scientific notation. Since your dates had either letters like JAN or dashes then another informat is needed.

The Lost Card usually means that you tried to read more data than provided in one way or another. Your data line only has one value per line and two lines. Your input statement tries to read 3 values. So you ran out of data.

The Rule is an indicator of how SAS sees the line that you attempted to read, in this case when the data ran out. The _N_=1 says this happened the first time your Input statement executed. Note that your variable names are written followed by =. That means the error(s) to that point resulted in all the value having a missing values.

The Note about "SAS went to a new line" is pretty specific to attempting to read more values from a single line of data in the datalines than exists. You have one "value" and read 3 lines. There are options that control this behavior. SAS is only using a NOTE because sometimes that is what we want with some (usually poorly created) data files. If your values had been valid for numeric you would have seen two numeric values for the date variables and a missing value for the dayslived (because) there was nothing to read.

 

Work on reading the dates first. Then worry about calculating the Dayslived (trivial if your read the working with dates above).

Find the format you want to display the values and the Proc Print part is easy.

 

 

If you haven't bookmarked the online help here it is: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=pgmsashome&docsetTarget=h...

 

You can search for things like INFORMAT or FORMAT (there is lot to find) on the help site, or search on the Forum for those keywords to find examples of using them. Or at least one in ten questions on the forum (should be closer to nine of ten) will have example data steps, often reading dates.

 

Patrick
Opal | Level 21

@Godzilla_Hat Make sure you spend the time reading what's in the article and links in it that @ballardw provided. If you want to use SAS then it's important that you understand how dates are stored and what functions there are to work with such SAS dates.

data elvis;
  infile datalines truncover;
  input bdate:date9. ddate:mmddyy10.;
  format bdate ddate date9.;
  days_lived=ddate-bdate;
  datalines;
08JAN1935 08-16-1977
;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 379 views
  • 1 like
  • 3 in conversation