Import Question

Reply
Occasional Contributor
Posts: 10

Import Question

Hi, I have a bunch of tab-delimited text files that have the following format:

var names

var labels

data

When I proc import, the labels are brought in as the first line of data. I was wondering if there was anyway for SAS to make these labels instead, or better yet, to test that the second line is/isn't labels each time it imports.

Thanks.

Super User
Posts: 11,343

Re: Import Question

Posted in reply to united_mdm

If they are all in the same format then I would take the code generated, it should show in the log, and modify it to use the option firstobs=3 on the INFILE statement and then use the labels in the data to add labels to the data.

It might simplify the project to make one copy of the data with the label line deleted so that data types are chosen more appropriately by the import generated code.

To simplify making the label code you can try opening the file in a spread sheet and copy the first two lines the post in another sheet to transpose to columns. If familiar with concatenation in the spread sheet you can then make one column with

var = "label"

to copy  and paste into a label statement.

Because I want to control length of character variables, informats, formats and labels I seldom use the default generated by proc import. If the files are supposed to contain similar data it is a good idea to look at the lengths of character variables and ensure they are long enough for values in all of the files.

Occasional Contributor
Posts: 10

Re: Import Question

Thanks ballardw. Yea, I was trying to avoid using the obs option, since I still need to keep the variable names. And I was hoping to do all this without having to manually change the text file since there are quite a few of them. My only thought right now is to delete the second observation with each import, but there's still the problem with all the number variables being in character format because of it.

Super User
Posts: 11,343

Re: Import Question

Posted in reply to united_mdm

Are all of the files in the same layout or each different?

Also, if you run proc import on file without the label row if you use the DATAROW=2 option then the numeric data should come in fine with the first row providing variable names.

Occasional Contributor
Posts: 10

Re: Import Question

@ ballardw: Ah, datarow=2 (or would it be 3?) seems like it will work. The labels aren't too important. But it would be nice to be able to assign them via something like labelrow=x.


@ data_null_;: There isn't any data documentation, but the data are nothing too crazy, and best32 should work just fine. But if you have any idea how I can assign a certain row in the file as labels, I would love to know.

Respected Advisor
Posts: 3,799

Re: Import Question

Posted in reply to united_mdm

I would just write my own code to read the names and make labels from row 2.  Do you really want PROC IMPORT to guess at the data type?  Do you have documentation that tells you what the datatype of each variables is?

Respected Advisor
Posts: 3,799

Re: Import Question

Posted in reply to data_null__

With DATAROW=3 to skip the label row we can turn our attention to labels.  This is a bit fiddley with code generation but you could package the IMPORT and labels bit with a macro.


filename FT15F001 temp;
parmcards;
x,y,z
label of x,label of y,label of z
1,C,3
3,4,5
5,6,7
;;;;
  run;
proc import datafile=ft15f001 dbms=csv out=x replace;
  
getnames=yes;
   datarow=3;
  
run;
proc print;
  
run;

filename FT83F001 temp;
data _null_;
  
infile FT15F001 obs=2 missover dsd;
  
file FT83F001;
   array names[10] $32;
  
array labels[10] $128;
  
input names
  • / labels
  • ;
  •    do i = 1 to dim(names) while(not missing(names));
         
    put 'Attrib ' names 'Label=' labels :$quote130. ';';
         
    end;
      
    run;

    proc datasets;
      
    modify x;
       %inc Ft83F001;
       run;
      
    contents data=x;
       run;
      
    quit;
    Ask a Question
    Discussion stats
    • 6 replies
    • 278 views
    • 3 likes
    • 3 in conversation