Luca
Luca

Reputation: 61

How to import a fixed-width text file (with many variables) which the layout is stored in a SAS File (.sas)?

I cannot understand how to work with the following files:

I already posted a question about this and seems that the only way to merge variables' names and values is to do it manually. The problem is that I've been trying to figure this out for days now and often made mistakes.

Does anyone have an idea of how to solve this problem and automize importation with python pandas?

The .sas file (as opened in WPS workbench) appears as follow:

DATA YOUR_DATA;
INFILE 'C:\Users\...\file.txt';
    
 ***Column   VarName   Varlength      VarLabel***
    @18      PROFAM    6.             /*  progressivo famiglia univoco a livello indagine                                                                                                                                                           */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    @24      PROIND    2.             /*  progressivo individuo nell'ambito della famiglia                                                                                                                                                          */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    @29      NCOMP     2.             /*  n° dei componenti la famiglia attuale                            

Where Column VarName Varlength VarLabel are not in the real code and have been added here to explain the code.

Upvotes: 1

Views: 1246

Answers (1)

LuizZ
LuizZ

Reputation: 1044

One way you can do it is:

  1. Download the WPS Workbench Community edition. WPS Workbench is a software that read sas files. If you are enrolled in some College/University, you can also download the academic edition (which is free of ads and have extra features). After you install and set it up, open it.

  2. In WPS Environment, open the .sas program: by clicking in File -> Open File and selecting your .sas file, which will open in WPS. Set three lines like that:

DATA YOUR_DATA;
INFILE 'C:\Users\...\path_to_your_file\YOURDATA.TXT' LRECL=978 MISSOVER;
INPUT
 @18  PROFAM  6.  /*  progressivo famiglia univoco a livello indagine */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 @24  PROIND  2.  /*  progressivo individuo nell'ambito della famiglia*/ 

Just choose the name you want for your data in the first line and put the correct path to your .txt file in the second line.

2.1 Correct the code by: Erasing the line ***Column VarName Varlength VarLabel*** that is between the INPUT statement and the first line of data.

2.2 Check the last two lines after the last variable, it should end something like that:

@235 NCOMPL  8. /*  n° rendimento la famiglia attuale */
;
run;

If it is not, add the last two lines above.

  1. Write the syntax to export your data to a .csv. Just write the following code at the end of the .sas program file:
proc export data=YOUR_DATA
     outfile="c:\myfiles\YourData.csv"
     dbms=csv 
     replace;
run;

Make sure to write again the correct name of your file (the one you chose) in the first line, and the correct path where you want the .csv file to be saved in the second line.

Press ctrl + R to run the code, or click on the run button. Click Ok in the Save and run WPS dialog box.

  1. Finally, open the .csv in Pandas.

Upvotes: 1

Related Questions