BookmarkSubscribeRSS Feed
united_mdm
Calcite | Level 5

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.

6 REPLIES 6
ballardw
Super User

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.

united_mdm
Calcite | Level 5

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.

ballardw
Super User

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.

united_mdm
Calcite | Level 5

@ 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.

data_null__
Jade | Level 19

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?

data_null__
Jade | Level 19

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;

    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!

    How to Concatenate Values

    Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

    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
    • 6 replies
    • 841 views
    • 3 likes
    • 3 in conversation