DATA Step, Macro, Functions and more

remove last 3 characters

Reply
Regular Contributor
Posts: 196

remove last 3 characters

I want to remove the last 3 characters (i.e. :00) from a variable.
For example, how do I change these 3 observations:
8:00:00
7:45:00
10:00:00

to this format
8:00
7:45
10:00

Thank you.
Occasional Contributor
Posts: 8

Re: remove last 3 characters

Here are a few options:
data t;
infile cards;
input t $8.;
length solution1 solution2 solution3 $5.;
solution1=scan(t,1,':')||':'||scan(t,2,':');
solution2=put(input(t,time8.),time5.);
solution3=reverse(substr(reverse(trim(left(t))),4));
cards;
8:00:00
7:45:00
10:00:00
;
Super User
Posts: 9,682

Re: remove last 3 characters

What type is your variable? Numeric or Character?
[pre]



data t;
infile cards;
input t $8.;
time=put(input(t,time8.),hhmm5.);
cards;
8:00:00
7:45:00
10:00:00
;
run;
[/pre]


Ksharp
Regular Contributor
Posts: 196

Re: remove last 3 characters

Numeric.
Your response and p12937's response were very helpful.
Thank you.
Respected Advisor
Posts: 3,895

Re: remove last 3 characters

Hi

If the type is numeric then assigning another format like Ksharp showed would do:

format time hhmm5.;

HTH
Patrick
Ask a Question
Discussion stats
  • 4 replies
  • 3375 views
  • 0 likes
  • 4 in conversation