i have data from 15 th row onwards
15th row-----column names
16 the row----data exists
i tried this code
proc import datafile=' '
out=
dbms=csv
replace;
DATAROW=16;
run;
How to get column names?
in the output table it is showing var1 var2 ............................................
i want column names also
Hmmm, not so fast. Noticing the variable names are on 15th row, while getnames=yes will take the first row as the variable names. I suspect that proc import probably can't meet OP's need on this aspect.
Regards,
Haikuo
Hi,
Try this...but not by using proc import
data WORK.TEST1 ;
infile 'C:\Documents and Settings\test\Desktop\test1.csv' delimiter = ',' MISSOVER DSD
lrecl=32767 firstobs=16 ;
informat name1 $6. ;
informat no best32. ;
format name1 $6. ;
format no best12. ;
input
name1 $
no
;
run;
Thanks,
Shiva
It should work. When getnames=yes, the default value for datarows is 2. So if datarows=16 then the variable names should come from row 15 when getnames=yes.
Are you sure about that?
proc import datafile='h:\test.txt' out=test
dbms=csv
replace;
getnames=yes;
datarow=5;
run;
Regards,
Haikuo
I was wrong. Just tested it myself. Getnames always gets the variable names from the first row.
So it looks like the proc import can not really meet what you need. if you can not modify the input csv file, you could try data step to do the job:
filename test 'h:\test.txt';
data _null_;
infile test dsd truncover firstobs=15 ;
input;
call symputx('in',translate(_infile_,' ',','));
stop;
run;
data want;
infile test dsd truncover firstobs=16;
input (&in) (:$20.);
run;
This will give you the variable names, however, they will be all character type with the same length of 20.
Regards,
Haikuo
filename x 'c:\data.csv'; filename xx temp; data _null_; infile x; file xx; input; if not missing(_infile_) then put _infile_; run; proc import datafile=xx out=test dbms=csv replace; getnames=yes; run;
Ksharp
Use the FIRSTOBS option on the INFILE X statement to skip over the lines before the data block. That eliminates the need to skip blank lines and also handles cases where the lines to be skipped are not blank.
Yeah. But you need to know which obs should be started, if it is varying ?
For the missing obs, it is usually unuseful. thus delete them is not a bad idea.
Ksharp
It is not hard to read the name row yourself and generate code that will rename the default VAR1,VAR2,.... that IMPORT uses when GETNAMES=no.
The data step below will read the name and generate a macro variable with the value needed for a rename statement or rename dataset option.
%let datarow=6;
%let namesrow=5;
data _null_;
length rename $32767 name $32 ;
infile tmpfile1 firstobs=&namesrow dsd truncover;
do _n_=1 by 1 until (name = ' ');
input name @@ ;
if name ne ' ' then
rename=catx(' ',rename,catx('=',cats('var',_n_),name));
;
end;
call symputx('rename',rename);
stop;
run;
proc import file=tmpfile1 out=test1(rename=(&rename))
dbms=csv replace ;
datarow=&datarow;
getnames=no;
run;
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.