BookmarkSubscribeRSS Feed
sasemp999
Calcite | Level 5

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

10 REPLIES 10
Haikuo
Onyx | Level 15

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

shivas
Pyrite | Level 9

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

FloydNevseta
Pyrite | Level 9

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.

Haikuo
Onyx | Level 15

Are you sure about that?

proc import datafile='h:\test.txt' out=test

dbms=csv

replace;

getnames=yes;

  datarow=5;

run;

Regards,

Haikuo

FloydNevseta
Pyrite | Level 9

I was wrong. Just tested it myself. Getnames always gets the variable names from the first row.

Haikuo
Onyx | Level 15

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

Ksharp
Super User
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

Tom
Super User Tom
Super User

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.

Ksharp
Super User

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

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 10 replies
  • 1095 views
  • 1 like
  • 6 in conversation