Help using Base SAS procedures

data

Reply
Occasional Contributor
Posts: 14

data

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

Respected Advisor
Posts: 3,124

Re: data

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

Super Contributor
Posts: 349

Re: data

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

Frequent Contributor
Posts: 101

Re: data

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.

Respected Advisor
Posts: 3,124

Re: data

Are you sure about that?

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

dbms=csv

replace;

getnames=yes;

  datarow=5;

run;

Regards,

Haikuo

Attachment
Frequent Contributor
Posts: 101

Re: data

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

Respected Advisor
Posts: 3,124

Re: data

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

Super User
Posts: 9,691

Re: data

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

Super User
Super User
Posts: 6,502

Re: data

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.

Super User
Posts: 9,691

Re: data

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

Super User
Super User
Posts: 6,502

Re: data

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;

Ask a Question
Discussion stats
  • 10 replies
  • 403 views
  • 1 like
  • 6 in conversation