BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NKormanik
Barite | Level 11

A few days ago I made it this far, in creating a particular dataset:

 

data sas_1.test ;
infile "c:\2\gt\i_21503a.txt";
format Indicator $30. ;
format Parameter 12.2 ;
format Number 12.0 ;
format Success 12.2 ;
format Fail 12.2 ;
Input
Indicator $
Parameter
Number
Success
Fail;
run;

The code above creates just one of many such datasets.

 

For each of the datasets, I would like to include an additional variable that represents, basically, the 'path' to the infile .txt file -- the initial data source.

 

Let's call the new variable "Path".

 

So, in the case above, the "Path" variable would contain:

 

2_gt

 

Every line of data in the dataset would additionally include this "Path" variable (I would rearrange it to be the very first variable), with the same element all the way down, "2_gt".

 

Another dataset might have "Path", with all lines being "3_xy".

 

So, the question is, how to enter a new variable, with the same thing all the way down.

 

Too, can I include doing so in the code at top?

 

Suggestions greatly appreciated.

 

Nicholas Kormanik

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
NKormanik
Barite | Level 11

Indeed....

I see now, simply add the following just above "run;":

Path='2_gt';

Thanks to both of you for jogging me along.

 

View solution in original post

4 REPLIES 4
ballardw
Super User

You should read the documentation for the INFILE statement as there are lots of options. In this case you want Filename.

data sas_1.test ;
infile "c:\2\gt\i_21503a.txt" filename=tempfile;
length myfilevar $ 200;
myfilevar = tempfile; 
format Indicator $30. ;
<rest of data step to read data>

The option creates a temporary variable, one that will not get written to the data set. So you have to assign that value to the variable you want. And since this is character it is a good idea to take control of the length of the variable.

 

Note that if you are reading multiple files, such as with a wildcard character or explicit filename that points to multiple files the variable with change as the new files are read.

andreas_lds
Jade | Level 19

Untested:

/* add length after data-line */
length path $ 4 _filename $ 400;
 
/* change infile-statement */
infile "c:\2\gt\i_21503a.txt" filename=_filename;
/* .... */

path = cats(scan(_filename, 2, '\'), '_', scan(_filename, 3, '\'));

run;
NKormanik
Barite | Level 11

I'll add in the actual 'string' myself.  I don't at present want SAS to do that for me.

 

The brunt of the question is, can I change the top code shown to add in the 'string' variable.

 

I see now that I can change each of the individual dataset files in a separate procedure as follows:

 

data sas_1.i_21503a_short_gt;
set sas_1.i_21503a_short_gt;
Path='2_gt';
run;

This adds in the variable "Path", with each component "2_gt".

So, again, is it possible to adjust the

very top code to do the same?

 

NKormanik
Barite | Level 11

Indeed....

I see now, simply add the following just above "run;":

Path='2_gt';

Thanks to both of you for jogging me along.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1236 views
  • 2 likes
  • 3 in conversation