BookmarkSubscribeRSS Feed
jpate242
Calcite | Level 5

Hello,

 

I'm brand new to SAS and coding in general. I tried searching on youtube and all the other place on "how to import txt file into sas studio" but nothing is helpful. Can you guys tell me how can I read the txt file on the studio? Also is it possible to have csv and txt files on the same edition(code)?

 

if so, what are the steps?

 

Thanks,

6 REPLIES 6
Tom
Super User Tom
Super User

How are you using SAS/Studio?  Are you using it to connect to the free SAS University Edition of SAS running in a virtual machine?  Or are you using a commercial version.  If the later where is the SAS that you are connecting to with SAS/Studio running?  Is it PC SAS running on your machine? Or SAS running somewhere else?

 

What do you mean be IMPORTING a file?  Are you wondering how to place the file where you SAS code (and SAS/Studio interface) can find it?  Or are you already able to find the file and are wondering about how convert it into a SAS dataset so that you can use it for analysis?

jpate242
Calcite | Level 5

Im using it through the VM and running the sas studio. I would like to use the infile and iput to read the file. 

Tom
Super User Tom
Super User

@jpate242 wrote:

Im using it through the VM and running the sas studio. I would like to use the infile and iput to read the file. 


In that case the easiest way to get the files where SAS can see them is to put them into the folder (directory) on your machine that you shared with the VM.  Then SAS will see them under the location /folders/myfolders/ that you setup when you configured the VM.

There is also an UPLOAD menu item in the SAS/Studio interface to help with moving files from the machine you are using to connect to the SAS/Studio interface and the machine where SAS is running.  But it is not needed here since the VM is on the same machine so it can immediately see any file you place into the right location.

 

Now on reading the data there is a way in SAS/Studio to point and click you way to generating PROC IMPORT code.  Or you could just write the PROC IMPORT code yourself.  Or just skip PROC IMPORT completely and write your own data step to read from the text file.  When PROC IMPORT runs it generates data step code to read the file.  So you can look at that to get ideas about how to read the file.  Note that the code PROC IMPORT generates is typically much more verbose than it needs to be since it is written by an algorithm instead of a human.

ballardw
Super User

@jpate242 wrote:

Hello,

 

I'm brand new to SAS and coding in general. I tried searching on youtube and all the other place on "how to import txt file into sas studio" but nothing is helpful. Can you guys tell me how can I read the txt file on the studio? Also is it possible to have csv and txt files on the same edition(code)?

 

if so, what are the steps?

 

Thanks,


"Text files" can have lots of variations. So the main thing is knowing what type of text file you have. CSV are text files but there are some expected behaviors with them, mainly that commas (though some folks use the C for "character" and use other characters) as delimiters between values. So if your "text file" uses a comma to separate values and the order of columns (what the values mean) are in the same order it may be possible to read a text file and CSV file with the same code. Maybe.

 

One thing when asking help is to provide concrete examples of what you have and code that you may have attempted. To provide an example of a text file (or csv) copy the first 10 lines or so when viewing the file with a plain text editor (the SAS editor is one) and paste the result into a code box opened on the forum with the {I} icon.

 

 

The basic idea for reading data is that you know 1) what order your variables(columns) are, 2) what type of data, numeric, character or date/time/datetime value and 3)  how long values need to be. Best is to have a document that describes the file you are reading as that should provide the information that you need.

A note to a beginner: many values that consist only of digits should be treated as character. The general rule is if you don't think that you will do arithmetic with it then it should be character. Common items that fall into this category are any sort of identification number, account number, phone number and such.

If you have one or more columns that contain values that should be numeric for calculations sometime and character other times then you likely will have some challenges.

 

If your file is relatively nice you may be able to use PROC IMPORT or an import wizard or task. But you would need to know if it is delimited and what the delimiter might be. Caution: these approaches examine each file and may assign different properties to similar structured files because the contents change. Commonly the changes would revolve around length of character variables but the "all digit" identifiers mention above are likely to be created as numeric. Which is a problem if the values have significant leading zeroes.

jpate242
Calcite | Level 5

this is my code

 

filename main '/folders/myfolders/Practice/main.txt';

data = main;
infile main dlm = ' ';
input x y z ;
run;

 

and its giving me an error

Tom
Super User Tom
Super User

@jpate242 wrote:

this is my code

 

filename main '/folders/myfolders/Practice/main.txt';

data = main;
infile main dlm = ' ';
input x y z ;
run;

 

and its giving me an error


You need to show the actual lines from the SAS log to get detailed help.  Note on using this forum: Use the pop-up window you get when clicking on the Insert Code or Insert SAS Code icons to enter example text (or log lines) and example program code, respectively.

 

The DATA statement does not want an equal sign.  You do not need to tell SAS to use space as the delimiter, it is the default.  

Try:

filename main '/folders/myfolders/Practice/main.txt';

data main;
  infile main truncover;
  input x y z ;
run;

If you want to use something else as the delimiter then normally you also want to use the DSD option so that adjacent delimiters are treated as marking a missing value. If you want to use a TAB as delimiter then you are better off use the hex code for a tab character instead of an actual tab characters.  Most reasonable program editors will replace tabs with spaces to allow code to be portable to users that might prefer different tab stops than you do. 

data main;
  infile main dsd dlm='09'x truncover;
  input x y z ;
run;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1270 views
  • 1 like
  • 3 in conversation