BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JamDOT
Calcite | Level 5

I have tried the following in Windows 9.3:

 

data want;
set have;
Varnew=input(Varold,time.);
FORMAT Varnew time5.;
run;

 

The procedure runs, but only one observation and two variables result when there should be 1000s.

 

 

NOTE: Numeric values have been converted to character values at the places given by:
(Line):(Column).
548:16
NOTE: There were 1 observations read from the data set have.
NOTE: The data set want has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.04 seconds

 

The data is 4 characters long and in military time. I ultimately want to convert the observations to numeric format so I can group them by the hour. They are currently by the minute.

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

You can use the HHMMSS. informat to read a hhmm char value and translate it to a SAS time value.

 

See code below:

data t;
  do charTime =
    "0001"
    ,"0002"
    ,"0003"
    ,"2356"
    ,"2357"
    ,"2358"
    ,"2359"
  ;
  sasTime = input(charTime, hhmmss4.);
  output;
 end;
 format sasTime tod8.;
run;

View solution in original post

12 REPLIES 12
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I don't know what you mean by military time?  This is where showing example data helps.  

JamDOT
Calcite | Level 5

The character data is 4 characters long:

 

0001

0002

0003

....

2356

2357

2358

2359

 

I want to convert this character variable to a numeric time format.

 

 

Shmuel
Garnet | Level 18

From your log lines

NOTE: There were 1 observations read from the data set Varold.
NOTE: The data set Varnew has 1 observations and 2 variables.

 

it seems that you have not psted the right code.

According to posted code varold and varnew are variables not datasets !

JamDOT
Calcite | Level 5

you are correct:

 

NOTE: Numeric values have been converted to character values at the places given by:

(Line):(Column).

548:16

NOTE: There were 1 observations read from the data set have.

NOTE: The data set want has 1 observations and 2 variables.

NOTE: DATA statement used (Total process time):

real time 0.03 seconds

cpu time 0.04 seconds
Rick_SAS
SAS Super FREQ

Your log messages do not match the program that you submitted.

 

The TIME. format displays time in 'military time' (24-hour clock) but does not print a leading zero for 06:00.

If you want the leading zero, use the TOD. format.

 

I suggest you use the ANYDTTME. informat to convert the strings to a time value, as follows:

 

data t;
length s $8.;
input s;                 /* a string */
t = input(s, ANYDTTME.); /* a (numeric) time value */
format t tod8.;          /* tell SAS to format the number as a 24-hour time */
datalines;
23:02:03
22:57:16
06:00:00
04:40:22
run;

proc print;run;

 

JamDOT
Calcite | Level 5
why do datalines need to be included? Isn't the data being fed from 't'?
Rick_SAS
SAS Super FREQ

I used DATALINES so that everyone could see the data.  If you have a data set 'have' that contains the variable s, you can use

 

 

data t;
SET have; /* the 'have' dataset contains the character variable s */
t = input(s, ANYDTTME.); /* a (numeric) time value */
format t tod8.; /* tell SAS to format the number as a 24-hour time */
run;

 

JamDOT
Calcite | Level 5

605 data want;

606 set have;

607 Varnew=input(varold,anydttme.);

608 FORMAT varnew tod8.;

609 run;

NOTE: There were 1167321 observations read from the data set have.

NOTE: The data set new has 1167321 observations and 113 variables.

NOTE: DATA statement used (Total process time):

real time 3.88 seconds

cpu time 3.52 seconds

 

610 proc freq data= want;

611 tables varnew/nocum;

612 run;

NOTE: There were 1167321 observations read from the data set WORK.TRAVELTIME.

NOTE: PROCEDURE FREQ used (Total process time):

real time 1.24 seconds

cpu time 1.21 seconds



The FREQ Procedure endtimeN Frequency Percent [][][] /*3 column table here with blank cells*/ Frequency Missing = 1167321

  The output table still produces no data, BUT at least all observations are now being read.

Rick_SAS
SAS Super FREQ

If the data are really of the form 

0001

0002

etc

then those are invalid data strings, which is why the informat is converting them to missing. Try creating a new string that inserts a colon between the second and third characters, like this:

 

data t;
length varOld $4. s $5.;
input varOld;                 /* a string */
s = catx(":", substr(varOld,1,2), substr(varOld,3,2));
t = input(s, ANYDTTME.); /* a (numeric) time value */
format t tod8.;          /* tell SAS to format the number as a 24-hour time */
datalines;
0001
0002
0003
2356
2357
2358
2359
;

proc print; run;
JamDOT
Calcite | Level 5

it works with datalines but when I put the set statement in, I get errors again.

 

24   data want;
25   set have;
26   length varold $4. varnew $5.;
27   input varold;
28   varnew =catx(":", substr(varold,1,2),substr(varold,3,2));
29   want=input(varnew,anydttme.);
30   format want tod8.;
31

ERROR: No DATALINES or INFILE statement.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set want may be incomplete.  When this step was stopped there were
         0 observations and 114 variables.
WARNING: Data set want was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.04 seconds

32   proc print;
33   run;

NOTE: There were 1 observations read from the data set want.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds

BrunoMueller
SAS Super FREQ

You can use the HHMMSS. informat to read a hhmm char value and translate it to a SAS time value.

 

See code below:

data t;
  do charTime =
    "0001"
    ,"0002"
    ,"0003"
    ,"2356"
    ,"2357"
    ,"2358"
    ,"2359"
  ;
  sasTime = input(charTime, hhmmss4.);
  output;
 end;
 format sasTime tod8.;
run;
JamDOT
Calcite | Level 5

where in the code does the data file go if I am not using datalines (very much a novice here)

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 12 replies
  • 2639 views
  • 3 likes
  • 5 in conversation