SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1943 views
  • 1 like
  • 3 in conversation