Proc Import always changes last column name to VARx - sas

I am trying to import a csv using proc import.
proc import datafile='/SourceFiles/UserTable.csv'
out=UserTable dbms=csv replace;
getnames=yes;
run;
The column names are captured correctly except for the last. The last column always changes to VARx. For testing purposes, I even change my dataset to have one column and one value so that it's like
USER
Johnson
But USER changes to Var1 as well. I'm pretty sure I'm not violating any naming conventions.
Anybody have any ideas?

try this, it worked for me using sas 9.3
proc import datafile="C:\Users\OldSalt\Desktop\test.csv"
out=mydata
dbms=csv
replace;
getnames=yes;
run;
So, this looks the same as your script. My is running just fine on a PC. It looks like you are running on a UNIX box. Check your input file. It might be corrupted.

Related

Is there a way to import multiple excel sheets using code in SAS Enterprise when DBMS errors occur?

I wrote some code with the hope of importing multiple sheets, but I receive an error:
"ERROR: DBMS type XLSX not valid for import."
I looked into the error and tried XLS etc, and then checked
proc setinit; run;
The output did not include "---SAS/ACCESS Interface to PC Files" in which case I see that the advice is to use a csv file instead. That would be fine, except I am trying to import dozens of sheets in a macro (see below) from a single excel workbook. I'm not sure that it makes sense to save each sheet as a csv so that the macro can pull in all of the files. That would be labor intensive in the future, as these sheets will be updated maybe multiple times a year.
%let path = 'C:/Desktop/Folder';
%macro importsheet(sheet);
proc import out= &sheet
datafile = &path
dbms=XLSX replace;
getnames=yes;
run;
%mend;
%importsheet(sheet1);
My core question is this:
Is the csv option the only/best solution for what I am asking? My back up plan is to just do an autoexec, but I would like to make sure I've thoroughly explored writing a macro and discarded it as an option before I do that.
The fastest and easiest way to import all Excel Sheets is to use a libname reference.
libname myXL xlsx 'path to xlsx file';
proc copy in=myXL out=work; run;
This will not work if you're importing from a specific range.

How to find out all Excel worksheets' names in SAS without using pcfiles or ExcelCS libnames?

I have been trying to import a large Excel file in SAS consisting 20 worksheets. I am using following macro for proc import
%macro excel_imp(outds, worksheet);
proc import
out=&outds
datafile= "Z:\temp\sample"
dbms=XLSX replace;
sheet="&worksheet";
getnames=yes;
run;
%mend excel_imp;
%excel_imp(Ds1,Worksheet1);
%excel_imp(Ds2,Worksheet2);
The above code is running fine, but I have to call the macro 20 times with separate worksheet names.
I would like an automated code to identify the worksheet names and then use the macro above. I don't have pcfiles/ExcelCS in my SAS EG, I am using 9.4
Appreciate any help! Thanks.
Since XLSX clearly works, why not use the XLSX libname.
libname demo xlsx 'path to xlsx file';
proc copy in=demo out=work;
run;

How do I import a census table into SAS when the variable name has special characters in it?

I am using SAS Enterprise Guide, importing American Community Survey tables from the census into a script to work with them. Here is an example of a raw census csv I'm importing into SAS Enterprise Guide:
within my data step, when I use the command
County=Geo.display-label;
I get this error:
In base SAS, I was using
County=Geo_display_label;
While that worked in base SAS, when I tried that in Enterprise Guide, I got this error:
What is a way to get the raw data's variable name Geo.display-label to read into SAS Enterprise Guide correctly?
To see the impact of the VALIDVARNAME option on the names that PROC IMPORT generates when the column headers are not valid SAS names lets make a little test CSV file.
filename csv temp ;
data _null_;
file csv ;
put 'GEO.id,GEO.id2,GEO.display-label';
put 'id1,id2,geography';
run;
If we run PROC IMPORT to convert that into a SAS datasets when VALIDVARNAME option is set to ANY then it will use the column headers exactly, including the illegal characters like period and hyphen. To reference the variables with those illegal characters we will need to use name literals.
options validvarname=any;
proc import datafile=csv replace out=test1 dbms=dlm;
delimiter=',';
run;
proc contents data=test1; run;
proc freq data=test1;
tables 'GEO.display-label'n ;
run;
But if we set the option to V7 instead then it will convert the illegal characters into underscores.
options validvarname=v7;
proc import datafile=csv replace out=test2 dbms=dlm;
delimiter=',';
run;
proc contents data=test2; run;
proc freq data=test2;
tables geo_display_label ;
run;
County = 'geo.display-label'n;
if you set OPTIONS VALIDVARNAME=V7; in EG you will get the same names as batch sas.

truncated headers with SAS during proc import

I'm having large headers for my columns like the one below (it is only a sample as I have 2000 columns with as many headers).
Each column is separated by a semi-colon.
BAL_RT,ET-CAP,EXT_EA16,SEXL-SA,UK;BAL_RT,ET-CAP,EXT_EA16,IBON-SA,TA;BAL_RT,ET-CAP,EXT_EA16,TARO-SA,XR
1;7.2;3
35;8;0.99
I'm using the following command line in SAS to do the import
options macrogen symbolgen ;
PROC IMPORT OUT= Work.fic38_fic1
DATAFILE= "C:\cygwin\home\appEuro\pot\fic38.csv"
DBMS=DLM REPLACE;
DELIMITER='3B'x;
GETNAMES=YES;
DATAROW=2;
GUESSINGROWS=32767;
RUN;
proc sort data=Work.fic38_fic1 ; by date ; run ;
However, for some unknown reasons, the headers got truncated.
BAL_RT,ET-CAP,EXT_EA16,SEXL-SA;BAL_RT,ET-CAP,EXT_EA16,IBON-SA;BAL_RT,ET-CAP,EXT_EA16,TARO-SA
I read the internet and they were talking about the option LRCL.
Does it make sense to anyone?
Any help will be appreciated.
Cheers.
It sounds like the issue is actually that you have 34-50 character wide variable names. SAS has a maximum of 32 characters for variable names, so you will not be able to use the entire length in the variable name. You may be able to use it as a variable label, but likely you would need to code that yourself if PROC IMPORT isn't going to do it for you. You could take the code out of the log and use that code with the additional text added by hand if you like.

Proc Import from SPSS

Relatively new to SAS and I'm using proc import on an SPSS (.sav) data file and it runs fine but I noticed that it brings in only the SPSS value labels rather than the numeric equivalent. As an example in the Gender column 1='male', 2='female' and in the SAS data set 'male' and 'female' show up rather than 1 or 2.
Any insight would be appreciated. Current code...
proc import datafile = "C:\Data\workload_20130314.sav"
out=library.workload_20130314
dbms = sav
replace;
run;
You probably have the underlying values there, they're probably just formatted. Try opening the dataset and viewing the column properties of one of the columns you're looking at; it probably has a format that's like Q49F. or something that does that. It still works with PROC MEANS or whatever as a numeric variable.
You can run, I think, something like
proc datasets;
modify my_dataset;
format _all_;
quit;
to remove the overlay. You can also do that on a case by case basis.