Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Milliseconds in SAS

Status
Not open for further replies.

marksnape

Programmer
Aug 4, 2008
5
GB
Hi,

I have a time series of data with time values to the nearest millisecond (eg. 08:04:15.137). How can I alter this so it is only recorded to the nearest second? i.e. 08:04:15

Alternatively, I have data with multiple records within one second, for example, with x and time fields:

x time
1 08:04:15.137
2 08:04:15.147
3 08:04:15.159
4 08:04:15.161
5 08:04:15.168
6 08:04:15.199
7 08:06:53.345
8 08:06:53.486
9 08:06:53.727

I only want to keep the first observation within each second, in this instance the data would now be:

x time
1 08:04:15
7 08:06:53

How would I go about this?

Thanks
Mark
 
What is the datatype of the Time field? If it is a characater field, you can use substr().

Code:
data newdataset;
set olddataset;
time=substr(time,1,8);
run;

I guess if the column is numeric, you could change it to character before trying substr(). Someone else might have a cleaner solution if the column is numeric.

Dave
 
If it's numeric, you just change the format length.
I imagine that the format on the field would be a timew.d format, probably time12.3, change the format to time 12.0, that'll trim the milliseconds from being displayed.
To take the first record for each second, you'll need to change the time to a character field (easiest, most reliable way to trim it down for this purpose), so I would suggest doing this.
Code:
data dset2;
  set dset1;

  length time_var_txt $12;
  time_var_txt = put(time_var,time12.0);
run;
proc sort data=dset2;
  by time_var_txt time_var;
run;
data dset3;
  set dset2;
  by time_var_txt;

  if first.time_var_txt;
run;
This will just keep the first record for each second.
Enjoy.


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Cheers. A mixture of the two worked a treat for my data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top