BookmarkSubscribeRSS Feed
emilynomura
Calcite | Level 5

I have figured out how to import multiple SAS files (using the * operator). How can I output the same number of input files? I want them to have the same exact names. All I am doing is creating variable labels, but I really would like to avoid uploading each and every file separately. Any help is appreciated!

10 REPLIES 10
Reeza
Super User

If all you're doing is variable labels, then why not do that via PROC DATASETS instead. It's an attribute and you can change it without touching the main data sets. 

 


@emilynomura wrote:

I have figured out how to import multiple SAS files (using the * operator). How can I output the same number of input files? I want them to have the same exact names. All I am doing is creating variable labels, but I really would like to avoid uploading each and every file separately. Any help is appreciated!


 

ChrisNZ
Tourmaline | Level 20

Your question is unclear.

I suppose you are importing text files into SAS data sets, right?

What do you want to do next? Create one data set per text file? Create new text files with a different header? Change the variable labels in the data sets you just created?

Reeza
Super User

With any automation, you get it working for one and then generalize. Can you show what you're doing for one file and we can then help you scale it.

emilynomura
Calcite | Level 5
Sure! I'm guessing I need some sort of export statement?
filename nch10in '/e1n/nch10*.txt';
options nocenter varlidvarname=upcase;
----------------------------------------------------------
data nch.nch10;
infile nch10in lrecl=746 missover pad;
INPUT
[many statements]
label
[many statements]
run;
----------------------------------------------------------
proc contents data=nch.nch10 position;
run;
Reeza
Super User

Labels are not written to text files by default so you’ll need to explain what you’re trying to achieve here. I do have a program that exports data with two header rows, first is the names, second row is variable labels and third row on is data. Only application I’ve seen that accepts data in that form is ArcGIS. 

https://gist.github.com/statgeek/ae153e40af0d35dce02d1c8102ff3b94

 

Patrick
Opal | Level 21

@emilynomura 

So what's your end-goal? To have SAS tables or to have text files where you've replaced the text ("column name") in the header row with some other text ("column label")?

 

Please explain a bit more in detail for one file what you have and what you want as outcome. Ideally post some sample data for have and want - such sample data often helps to avoid misunderstandings.

Kurt_Bremser
Super User

@emilynomura wrote:
Sure! I'm guessing I need some sort of export statement?
filename nch10in '/e1n/nch10*.txt';
options nocenter varlidvarname=upcase;
----------------------------------------------------------
data nch.nch10;
infile nch10in lrecl=746 missover pad;
INPUT
[many statements]
label
[many statements]
run;
----------------------------------------------------------
proc contents data=nch.nch10 position;
run;

This code does not create an output file, only a SAS dataset and a report from it.

emilynomura
Calcite | Level 5

Yes, my bad. For clarification, my end goal is merely to batch convert .txt. to SAS files while keeping the input statement and the label statement I mentioned earlier.

Kurt_Bremser
Super User

You can read multiple text files with identical structure in one data step into one dataset. By using the FILENAME= option in the INFILE statement, you can keep the name of the originating textfile in a variable in the dataset:

data want;
length filename fname $200;
infile "path/name*.txt" filename=fname /* other options */;
input .....;
filename = fname;
run;

The second variable (filename) is necessary because fname will be made temporary and cannot be kept in the output dataset.

 

Later on, you can use the filename variable to make output in a data step that writes text files dynamic.

andreas_lds
Jade | Level 19

@emilynomura wrote:

I have figured out how to import multiple SAS files (using the * operator). How can I output the same number of input files? I want them to have the same exact names. All I am doing is creating variable labels, but I really would like to avoid uploading each and every file separately. Any help is appreciated!


Using the * in the infile-statement is only useful if you want to combine multiple files into one dataset. This is, btw, the recommended way to work with data that has the same form, having multiple datasets with the same structure later on in your programs will only result in being forced to write loops to process them all. So maybe you should re-think the process you want to implement.

 

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
  • 10 replies
  • 1050 views
  • 0 likes
  • 6 in conversation