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!
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.
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
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;
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.