BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RAVI2000
Lapis Lazuli | Level 10

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.

S_RAVI_0-1638406703663.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Look at TRANWRD to replace parts of a string. You're still adding data not modifying it.

This fails to factor in the code already there. It replaces your data and you need to explicitly control the fact that you then don't output it again with the next PUT statement.
if left(upcase(_infile_))^=:'DATA WORK.' and left(upcase(_infile_))=:'DATA ' then put = "data frmts.";

ADD COMMENTS TO YOUR CODE.

View solution in original post

15 REPLIES 15
Reeza
Super User

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++.


 

RAVI2000
Lapis Lazuli | Level 10
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.
Reeza
Super User

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.

 

Reeza
Super User
And is there a reason to have a FILENAME statement for ae2 but then use the full filepath in the code as well?
RAVI2000
Lapis Lazuli | Level 10

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.

S_RAVI_1-1638414087099.png

 

 

RAVI2000
Lapis Lazuli | Level 10
There is a typo in my above reply.

For infile statement I used "original" and for file I used "fixed".
RAVI2000
Lapis Lazuli | Level 10
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.
Reeza
Super User

@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?

RAVI2000
Lapis Lazuli | Level 10
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 left(upcase(_infile_))^=:'DATA WORK.' and left(upcase(_infile_))=:'DATA ' then put = "data frmts.";

/*or*/



if left(upcase(_infile_))^=:'DATA WORK.' and left(upcase(_infile_))=:'DATA ' then _infile_ = "data frmts." ;


put _infile_;
run;



One with PUT and another with _infile_ options
Reeza
Super User
Ok and what happened and what did you want to happen instead?

If you comment every line with what you think is happening that'll help clarify things. Please include your code in a code box as well to avoid formatting issues.

FYI - In general, its helpful if you state what you're trying to achieve (insert a libname and add a library reference for your code), what you've tried, how it didn't work (what did you get versus what you expect) you can make this a much faster process.
RAVI2000
Lapis Lazuli | Level 10
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.

Reeza
Super User
Look at TRANWRD to replace parts of a string. You're still adding data not modifying it.

This fails to factor in the code already there. It replaces your data and you need to explicitly control the fact that you then don't output it again with the next PUT statement.
if left(upcase(_infile_))^=:'DATA WORK.' and left(upcase(_infile_))=:'DATA ' then put = "data frmts.";

ADD COMMENTS TO YOUR CODE.
Reeza
Super User
One last thing - you don't need to add the libname reference to your data steps to these if you call the library USER.
SAS has a 'feature' where if you have a library called user, everything goes there instead of WORK. So if you want to modify a program to use a library you can add a LIBNAME at the top called USER to the same location and another LIBNAME to the bottom of the program to clear the USER library reference and anything in between gets saved to the library.
RAVI2000
Lapis Lazuli | Level 10
What path does USER library go to ? I open my sas datasets using universal viewer. I should be able to open them.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 15 replies
  • 1068 views
  • 1 like
  • 2 in conversation