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

Hi Gurus,

 

I am trying to read in a xlsx file, where one column has values like these:

 

Capture.PNG

 

After proc import, some values become the values shown below. Can someone tell me how to remain the original value? Thanks a bunch!!

 

Capture 2.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Is your original file XLSX? Or XML? I would consider converting it to CSV and importing that, though if you have quotes anywhere in there it may end up more messy.

View solution in original post

8 REPLIES 8
ScottBass
Rhodochrosite | Level 12

https://documentation.sas.com/?docsetId=acpcref&docsetTarget=p0jf3o1i67m044n1j0kz51ifhpvs.htm&docset...

https://documentation.sas.com/?docsetId=acpcref&docsetTarget=n0msy4hy1so0ren1acm90iijxn8j.htm&docset...

 

See MIXED option, then use a subsequent data step to convert "real" numeric data to numbers.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
YimingWeng
Fluorite | Level 6

Hi,

 

Thanks for your reply, this was one thing that i tried but i got this error message:

 

Anything i did wrong?

 

2.PNG

Reeza
Super User
Is your original file XLSX? Or XML? I would consider converting it to CSV and importing that, though if you have quotes anywhere in there it may end up more messy.
YimingWeng
Fluorite | Level 6

I was wondering if you will see my post and reply:)

 

Original file is google sheet actually, i downloaded as xlsx.

 

I converted as csv and it works well, thanks so much Reeza!

ballardw
Super User

Yet another lesson about the dangers of using spreadsheets to enter data and transport.

You have a very big clue as to what is going on in the picture. Note that some of the cells have values that are right justified like 99,100. If you check the cell format in Excel you will likely find that it is Number with the thousands separator set to comma.

The other cells did not do that because there were not 3 digits after the comma (so not valid numbers) or appeared within the brackets. The one with 161, 162, 163 also did not convert to numeric because of the space after the comma.

The cell with the value of 68 is likely of "General" format

 

If the data was actually entered directly into the spreadsheet then that is a part of the issue: allowing Excel to guess what you meant.

If the data was created somewhere and then opened and Saved by Excel that could modify values depending on the source file type.

SAS will only allow a variable to have a single type, numeric or character. Depending on the actual contents of the cell then the import engine will make "best guess" as to contents.

 

I would also check to see that you have "complete" values for records further down in the data file. You didn't say exactly how you are attempting to read this file but if using Proc Import then properties could be set with examining only 20 rows of data and if you have a value like [1234,1235,1236,1237] it may get truncated because none of the first rows had a value that long.

 

If this file started out as exported from some program and then was edited in Excel you may have to go back to that original file and try importing or reading a version that was not edited by Excel.

YimingWeng
Fluorite | Level 6

Thanks so much for the in-depth explanation for excel formatting.

 

The data is from a google app, IT created a google sheet for me as export, I then download as xlsx format. In terms of input restriction, it is almost like excel because front users can enter whatever in that field.

 

I tried Proc Import, not working, so I followed Reeza's tip to convert into csv and do data infile, now it's working, i just need to define all columns which is a little bit of work.

 

Ideally I should be connecting to the back end database directly to gain efficiency and accuracy in this matter, but I was told that SAS is not able to connect to GCP yet? So every week I am doing google sheet to xlsx to SAS data set conversion routine, lucky me:)

 

Again thanks for your help, at least now i have a temporary solution!

ScottBass
Rhodochrosite | Level 12

You can use Powershell and the Microsoft.ACE.OLEDB.12.0 provider to query Excel like a database (which it most certainly is NOT!)

 

You can then call the Powershell script from SAS using filename pipe, streaming the results as CSV.  The Excel workbook must be closed at the time or you'll get file locking issues.

 

You can use PROC IMPORT against that CSV to generate a data step, then cut-and-paste the results from the SAS log and edit as required.

 

Here's hoping the structure of your Excel file does not change over time. 

 

See here for the Powershell script:  https://github.com/scottbass/Powershell/blob/master/Scripts/Query-Excel.ps1

 

Hope this helps a bit...


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
YimingWeng
Fluorite | Level 6

Thanks so much for the detailed solution, I will follow your instruction and learn:)

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1113 views
  • 2 likes
  • 4 in conversation