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

Dear Experts,

 

I have a problem with time variables when I use "proc import" to import an excel file which contains time variables. Please see the attached excel file. 

My program is: 

PROC IMPORT OUT= work.Lab1 DATAFILE='test.xlsx' DBMS=xlsx REPLACE; getnames=yes; format Static_test_1 Static_test_2 time8.;

run; 

data lab2; set lab1;   timev=static_test_2;run;

proc print data=lab2; where timev=29701; run; /*no output printed */

 

When I check variable timev, it does have a value of 29701, but proc print log shows there is no observation for this value. However the other values are okay. Please let me know where I did wrong. Thanks a lot!

 

yangx

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The Cause: Excel stores time a fraction of a day. The EXCEL display format hides the fact that some of those fractions are repeating decimals and depending on how the value was created in Excel may have fractional values that actually include fractions of a second that are not displayed.

SAS uses number of seconds so there is a conversion issue. You may need to use a longer format to see decimal portions of time, such as best16. If the actual value is 29701.003   then using where timev=29701 does not match.

If you want your times to display consistently to match the Excel original appearance:

Save the Excel file as CSV using the Excel menu.

Import the CSV.

 

OR actually truncate the values after importing to remove any decimal bits.

 

When the file save as is done then the value written to the CSV is actual text like 05:15:23 and the TIME informat that SAS will use will create the appropriate number of seconds without a decimal fraction.

View solution in original post

7 REPLIES 7
pink_poodle
Barite | Level 11

timev = static_test_2; is not working. Your imported variable name is Static test 2. You need to rename it to static_test_2 first.

yangx
Obsidian | Level 7

Thanks for your reply, but it doesn't work. 

pink_poodle
Barite | Level 11

Ok, you may want to try this:

 

data lab2; set lab1;   timev='Static test 2'n;run;

proc print data=lab2; where timev=29701; run; 

 

yangx
Obsidian | Level 7

To be more specific, not all of the values are not working, only some of time value are not working, such as '8:15:01', '9:20:48'. Don't know why some of values can be found, some of values don't equal to what you see in the dataset.  thanks.

 

 

pink_poodle
Barite | Level 11

It works with the Enterprise Guide import code:

 

DATA WORK.lab1;
    LENGTH
        'Study ID'n        8
        'Static test 1'n   8
        'Static test 2'n   8 ;
    FORMAT
        'Study ID'n      BEST12.
        'Static test 1'n TIME8.
        'Static test 2'n TIME8. ;
    INFORMAT
        'Study ID'n      BEST12.
        'Static test 1'n TIME8.
        'Static test 2'n TIME8. ;
    INFILE 'C:\Users\User\AppData\Local\Temp\SEG7864\test-38d4f7e228594a3187ccc20e0a545450.txt'
        LRECL=14
        ENCODING="WLATIN1"
        TERMSTR=CRLF
        DLM='7F'x
        MISSOVER
        DSD ;
    INPUT
        'Study ID'n      : BEST32.
        'Static test 1'n : BEST32.
        'Static test 2'n : BEST32. ;
RUN;

data lab2; set lab1; timev='Static test 2'n;run;
proc print data=lab2; where timev=29701; run;

 

ballardw
Super User

The Cause: Excel stores time a fraction of a day. The EXCEL display format hides the fact that some of those fractions are repeating decimals and depending on how the value was created in Excel may have fractional values that actually include fractions of a second that are not displayed.

SAS uses number of seconds so there is a conversion issue. You may need to use a longer format to see decimal portions of time, such as best16. If the actual value is 29701.003   then using where timev=29701 does not match.

If you want your times to display consistently to match the Excel original appearance:

Save the Excel file as CSV using the Excel menu.

Import the CSV.

 

OR actually truncate the values after importing to remove any decimal bits.

 

When the file save as is done then the value written to the CSV is actual text like 05:15:23 and the TIME informat that SAS will use will create the appropriate number of seconds without a decimal fraction.

yangx
Obsidian | Level 7

Hello,

 

Thank you very much for your reply.  Now I understand where the problem is. I tried to use round function when I assign the time value to another variable timev, then SAS can find where timev=29701. 

 

xiumei

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 1284 views
  • 0 likes
  • 3 in conversation