Hello everyone,
I have been trying to edit a .sas file using data _null_.
filename original '/data/studies/project1/ae1.sas';
filename fixed '/data/studies/project1/ae2.sas';
data _null_;
infile fixed;
file '/data/studies/project1/ae2.sas' ;
input;
if _n_ = 1 then
do;
put @1 "libname frmts '/data/studies/project1/raw_dataset_20211025';" ;
end;
if left(upcase(_infile_))^=:'DATA WORK.' and left(upcase(_infile_))=:'DATA ' then put = "data frmts.";
put _infile_;
run;
I am trying to add a libname statement and give "frmts." permanent library in the last data set.
I tried the above code and am viewing it in notepad++.
when I add "frmts." to the datastep in going into the other line as below.
Personally, I wouldn't recommend reading and writing to the same file at once. That's problematic and difficult to debug and test. I would write the program to a new file while testing and then replace it once complete using FCOPY/FDELETE/RENAME functions.
This example successfully adds the libname to the top of a program. Hopefully that helps you get started.
filename original '/home/fkhurshed/Program 1.sas';
filename fixed '/home/fkhurshed/Program 1 V2.sas';
data _null_;
infile original;
file fixed ;
input;
if _n_ = 1 then do;
put "libname frmts'/data/studies/project1/raw_dataset_20211025';";
end;
put _infile_;
run;
@RAVI2000 wrote:
Hello everyone,
I have been trying to edit a .sas file using data _null_.
filename fixed '/data/studies/project1/ae.sas'; data _null_; infile fixed; file fixed ; input; if _n_ = 1 then do; put @1 ' '; end; /*if _N_ <= 1 then _infile_= "libname frmts'/data/studies/project1/raw_dataset_20211025';" ; */ put _infile_; run;
I am trying to add a libname statement and change a back slash(\) to forward slash(/) in the infile statement and give frmts. permanent library in the last data set.
I tried the above code and am viewing it in notepad++.
Seems like your intent in that one is to replace the line, whereas the first was more of an insert type function. You cannot use the same type of logic there.
FYI - edits are not shown in the forum, which is why I append posts and if you want to add new information, it's preferable if you add it as a new entry/reply not as Edit to your original question.
@RAVI2000 wrote:
Yes, you are right @Reeza. I tried what you said. The libname statement works. I have edited my post. Could you please take a look over again. I tried giving the same for "frmts." permanent library libname. Seems like it's going over to the other line.
Thank you Reeza.
I tried, the method you said by giving two different filename and it worked for libname statement line.
filename original '/data/studies/project1/ae1.sas';
filename fixed '/data/studies/project1/ae2.sas';
data _null_;
infile fixed;
file '/data/studies/project1/ae2.sas' ;
input;
if _n_ = 1 then
do;
put @1 "libname frmts '/data/studies/project1/raw_dataset_20211025';" ;
end;
if left(upcase(_infile_))^=:'DATA WORK.' and left(upcase(_infile_))=:'DATA ' then put = "data frmts.";
put _infile_;
run;
But when I tried to give the "frmts" libref in my last datastep it is going over to the next line as in the below picture.
@RAVI2000 wrote:
You are right! For the first I want to insert a line and I used PUT for it. For Second I just want to replace "data frmts." in that data step line. I tried using _infile_, but it's not working either.
What did you try?
filename original '/data/studies/project1/ae1.sas';
filename fixed '/data/studies/project1/ae2.sas';
data _null_;
infile fixed;
file '/data/studies/project1/ae2.sas' ;
input;
if _n_ = 1 then
do;
put @1 "libname frmts '/data/studies/project1/raw_dataset_20211025';" ;
end;
if left(upcase(_infile_))^=:'DATA WORK.' and left(upcase(_infile_))=:'DATA ' then put = "data frmts.";
put _infile_;
run;
I have tried the above code to insert libname and my libref in the last datastep. It worked for the libname. I am able to insert the libname statement in the .sas file. But I am unable to place libref "frmts" in the last datastep.
It come as below.
LOG: (PUT _infile_)
data work.AE_V2;
%let _EFIERR_ = 0;
infile './AE V2.txt' delimiter='09'x MISSOVER DSD lrecl=32767 firstobs=2 termstr=CRLF;
format SEQ $25.;
format INITIALS $2.;
format FORM $142.;
format TOXICITY 12.;
format TOXICITY_CATEGORY 12.;
INPUT SEQUENCE_NO_ INITIALS FORM TOXICITY TOXICITY_CATEGORY;
LABEL
SEQUENCE_NO_ = "Sequence No."
INITIALS = "Initials"
FORM = "Form"
TOXICITY = "Toxicity Code"
TOXICITY_CATEGORY = "Toxicity Category";
if _ERROR_ then call symput('_EFIERR_',1);
run;
data frmts.
data AE_FM;
set AE_V2;
format TOXICITY TOXICITY.;
format TOXICITY_CATEGORY TOXICITY_GROUP.;
run;
If you see in the last datastep, I tried adding/ replacing the "DATA FRMTS." in the last datastep line "data AE_FM". But, it was going in the upper line instead of replacing it. I want "DATA FRMTS.AE_FM" in the last datastep.
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.