BookmarkSubscribeRSS Feed
Itsmadz5
Calcite | Level 5

Hello, I am having problems with my data. I have my code below and when I do to my data table not all the numbers are correct under the weight column. Any ideas what I did wrong? Thanks!

Itsmadz5_1-1713825689444.pngItsmadz5_0-1713825640197.png

 

2 REPLIES 2
andreas_lds
Jade | Level 19

If you don't define a length,  a char variable can only store eight chars.

To read a date you have to define a numeric variable and use informat and format statements.

If you need more assistance, please provide code as text, insert it using the running-man-button.

 

ballardw
Super User

I'm too lazy to type more than a couple lines of data so lets examine what happens:

data junk;
   input trt $ date $ weight;
datalines;
1 11/17/2023 61.40833333
1 12/1/2023 60.875
;

simple Proc print shows:

 
trt date weight
1 11/17/20 61.4083
1 12/1/202

60.8750

 

As @andreas_lds said the default length of the character variables is limited to 8 so they get truncated.

 

The solution for the date values is to read them as a date value. If you want people to understand the dates then assign a format that people will understand. Like

data junk;
   input trt $ date :mmddyy10. weight;
   format date mmddyy10.;

datalines;
1 11/17/2023 61.40833333
1 12/1/2023 60.875
;

Which when printed

 
trt date weight
1 11/17/2023 61.4083
1 12/01/2023 60.8750

 

 The format affects the display of the numeric values as well. Proc Print defaults to a "usable" format but may not display all the digits.

You can force different appearance with different formats. For example this forces the display to use 8 decimals

Proc print data=junk noobs;
   format weight f15.8;
run;
 
trt date weight
1 11/17/2023 61.40833333
1 12/01/2023 60.87500000

 

 Note that there is a limit to the precision of numbers stored in SAS and depending on your system 14 or 15 decimal points are going to push it.

 

The DATE values really should use one of the date informats and assign a desired appearance date format. Note that the groups created by an assigned format are generally valid for reporting, analysis and many graphing tasks. So you can change a report to reflect Year, Year and Month,  Year and quarter, Year and week just by changing the format in a procedure.

Once you have actual dates (and datetime or time values) there are functions to extract bits like Year, Month, Day, Hour etc, plus determine the number of intervals between values or to increment values.

 

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

 

 

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 208 views
  • 0 likes
  • 3 in conversation