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
Indeed....
I see now, simply add the following just above "run;":
Path='2_gt';
Thanks to both of you for jogging me along.
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.
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;
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?
Indeed....
I see now, simply add the following just above "run;":
Path='2_gt';
Thanks to both of you for jogging me along.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.