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

Hello Experts,

 

I have a question, why, using the proc import, the second column is imported correctly (in numeric values), but the firts is imported with the character values ?

SASdevAnneMarie_0-1647966249793.png

 

My code is :

 

proc import 
	datafile= "xxxx\calculs"
	out=PROD_diff
	replace dbms=xlsx;
	getnames=yes;
	range="Frais";
run;

I added the file.

 

Thank you for your help !

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Well, something in your data causes PROC IMPORT to guess (because that's what it does, and - Maxim 31 - Computers Are Dumb) that these columns are character. So I did the usual remedy: avoid the crap file format (Excel) and use something sensible. I saved the spreadsheet to a csv file, uploaded it to my SAS On Demand, and ran this code:

data calculs;
infile "~/calculs.csv" dlm="," dsd truncover firstobs=2;
input
  CD_PRODUIT :$4.
  FRAIS_GESTION_OL_EURO :commax.
  FRAIS_GESTION_OL_C :commax.
  FRAIS_GESTION_OL_PROFIL :commax.
  FRAIS_GESTION_OL_CROISS :commax.
  FRAIS_GESTION_OL_ALLOC :commax.
  FRAIS_GESTION_OP :commax.
  FRAIS_GESTION_OC :commax.
;
run;

And Bob's your uncle.

View solution in original post

13 REPLIES 13
ballardw
Super User

For one or more reasons the engine reading the data thinks that at least one of the values in the first few rows of the data a character.

Without access to the XLSX file involved specifically can't tell.

Common things to look for: column header that occupies 2 or more rows

Something that is not a number in the column such as a value like "NULL" or "N/A"

Look to see if any of the "numbers" are left justified in the spread sheet. They may have been entered with an ' before the characters making them character and not numeric.

A character such a < , as might appear in a value like <0.0001

 

By default only the first 20 rows or so are used by Proc Import to set variable type so what ever is happening should be visible in the first 20 rows used.

Tom
Super User Tom
Super User

For an actual XLSX file it checks the whole column, not just some limited number of cells.

ballardw
Super User

@Tom wrote:

For an actual XLSX file it checks the whole column, not just some limited number of cells.


Probably still stuck in XLS thinking then.

SASdevAnneMarie
Barite | Level 11
I added the file.

Thank you !
Tom
Super User Tom
Super User

So if the whole column is empty SAS will create the variable as character with length 1.  

Is that what is causing you trouble?

ballardw
Super User

@SASdevAnneMarie wrote:
I added the file.

Thank you !

I'm not sure that the file you attached is the one you actually say you read. There are no values of the FRAIS GESTION OL-EURO variable that would in any reasonable way approach 0.008 (the 8.  E-3) . There are values near 0.006, 0.0096, 0.0075 and the 0.01 but nothing with an 0.008.

 

Long shot, make a copy of the file and clear all the formatting for all the cells and then read that file.

Tom
Super User Tom
Super User

You could try checking the SAS dataset to try and figure out which cell was character.  At least which character string that did make cannot be converted into a number.

Not sure what the variable name is since you just posted a photograph of your data.  Let's use VAR as the name to demonstrate the code.  So look of observations where VAR is not empty but its value cannot be converted to a number.

proc _null_;
  set PROD_diff;
  if not missing(VAR) and missing(input(strip(var),??32.)) then put _n_= var= ;
run;

 

SASdevAnneMarie
Barite | Level 11
Thank you Tom,

I can't find the proc _null_ :
ERROR: Procedure _NULL_ not found.
Kurt_Bremser
Super User

That is a typo. It should be

data _null_;

If you provide example data in readily usable form (data step with datalines), such mistakes can be found immediately upon testing.

Kurt_Bremser
Super User

There is something in that column in your range that makes IMPORT think it is not entirely numeric.

Find that first, then decide how to deal with it

  • change it in the spreadsheet
  • convert in a follow-on step
  • save the range to a csv file which you read with a DATA step, using an informat which correctly deals with the "special" values.
Kurt_Bremser
Super User

Well, something in your data causes PROC IMPORT to guess (because that's what it does, and - Maxim 31 - Computers Are Dumb) that these columns are character. So I did the usual remedy: avoid the crap file format (Excel) and use something sensible. I saved the spreadsheet to a csv file, uploaded it to my SAS On Demand, and ran this code:

data calculs;
infile "~/calculs.csv" dlm="," dsd truncover firstobs=2;
input
  CD_PRODUIT :$4.
  FRAIS_GESTION_OL_EURO :commax.
  FRAIS_GESTION_OL_C :commax.
  FRAIS_GESTION_OL_PROFIL :commax.
  FRAIS_GESTION_OL_CROISS :commax.
  FRAIS_GESTION_OL_ALLOC :commax.
  FRAIS_GESTION_OP :commax.
  FRAIS_GESTION_OC :commax.
;
run;

And Bob's your uncle.

SASdevAnneMarie
Barite | Level 11
Thank you, Kurt !

That works !

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
  • 13 replies
  • 12456 views
  • 8 likes
  • 4 in conversation