BookmarkSubscribeRSS Feed
eduarxj32
Calcite | Level 5

Hi, everyone. I just started using SAS. I don't know how to import a text file.

Here an example of my text file:

 

code;description;total

1;hi;201

2;hey;301

3;good;401

 

That's it. BTW I don't know the field length just that it's semicolon delimited.

 

I just tried this but it doesn't work:

 

 

data Nombre_cuentas_cualit_1001
infile "C:\proyecto_sas_bv\Nombre_cuentas_cualit_1001.txt" dlm=';';
input code--total;
run;

4 REPLIES 4
Reeza
Super User

Use PROC IMPORT. Check the log to see the code generated.

Or if you want to write a data step, look up the INFILE statement and see what options are available. FIRSTOBS tells SAS which line the data starts at.
You do need to explicilty list all your variables when you use this method though, your INPUT statement isn't valid here. If you listed all variables in a FORMAT or LENGTH statement ahead of time, that would work but you haven't in this case.

https://stats.idre.ucla.edu/sas/faq/how-do-i-read-in-a-delimited-ascii-file-in-sas/

 


@eduarxj32 wrote:

Hi, everyone. I just started using SAS. I don't know how to import a text file.

Here an example of my text file:

 

code;description;total

1;hi;201

2;hey;301

3;good;401

 

That's it. BTW I don't know the field length just that it's semicolon delimited.

 

I just tried this but it doesn't work:

 

 

data Nombre_cuentas_cualit_1001
infile "C:\proyecto_sas_bv\Nombre_cuentas_cualit_1001.txt" dlm=';';
input code--total;
run;


 

eduarxj32
Calcite | Level 5

Thanks for your answer. I just found this example in SAS documentation:

proc import datafile="C:\proyecto_sas_bv\Nombre_cuentas_cualit_1001.txt"
out=Nombre_cuentas_cualit_1001
dbms=dlm
replace;
dlm=';';
run;

But I can not see the error it says:
ERROR 180-322: Statement is not valid or it is used out of proper order.

Reeza
Super User
Post the exact log as it indicates where the error is, your code looks correct but it's likely a missing semicolon or an extra one somewhere.
Kurt_Bremser
Super User

Don't waste time with PROC IMPORT when reading text files, in the end you are faster writing the code yourself:

data want;
infile datalines dlm=";" dsd truncover firstobs=2;
input code :$1. description :$10. total;
datalines4;
code;description;total
1;hi;201
2;hey;301
3;good;401
;;;;

For your actual file, replace the DATALINES keyword in the INFILE statement with your filename, and remove the DATALINES4; block.

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!

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
  • 4 replies
  • 1042 views
  • 1 like
  • 3 in conversation