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.
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;
I don't know what you mean by military time? This is where showing example data helps.
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.
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 !
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
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;
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;
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.
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;
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
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;
where in the code does the data file go if I am not using datalines (very much a novice here)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.