Hi Guys,
I have an input sas program where i have all my libnames declared.I am reading that into a sas dataset.while doing that its reading commented out values too.And i don't want those commented code into my sas dataset.
I would like to remove the comments from my dataset whether the comments are made in one single line or multiple lines.
the code looks like this:
Libname sample1 "XYZ";
Libname sample2 "ABC";
/*Libname sample3 "asdfghjj";*/
Libname sample4 "uxcvdgh";
/*commented out
Libname sample5 "XYZasdf";
Libname sample6 "XYZasdfgb";**/
Libname sample7 "XYZdefrgevsdc";
Requirement:Without editing the source file i.e., above sample code i would like the output like this in sas dataset:
obs var1
1 Libname sample1 "XYZ"
2 Libname sample2 "ABC"
3 Libname sample4 "uxcvdgh"
4 Libname sample7 "XYZdefrgevsdc";
Thanks For the Help
Like this?
data HAVE;
input STATEMENT $80.;
cards4;
Libname sample1 "XYZ";
Libname sample2 "ABC";
/*Libname sample3 "asdfghjj";*/
Libname sample4 "uxcvdgh";
/*commented out
Libname sample5 "XYZasdf";
Libname sample6 "XYZasdfgb";**/
Libname sample7 "XYZdefrgevsdc";
;;;;
run;
data WANT;
set HAVE;
if STATEMENT =:'/*' then COMMENT+1;
if not COMMENT then output;
if reverse(trim(STATEMENT)) =:'/*' then COMMENT=0;
keep STATEMENT;
run;
Like this?
data HAVE;
input STATEMENT $80.;
cards4;
Libname sample1 "XYZ";
Libname sample2 "ABC";
/*Libname sample3 "asdfghjj";*/
Libname sample4 "uxcvdgh";
/*commented out
Libname sample5 "XYZasdf";
Libname sample6 "XYZasdfgb";**/
Libname sample7 "XYZdefrgevsdc";
;;;;
run;
data WANT;
set HAVE;
if STATEMENT =:'/*' then COMMENT+1;
if not COMMENT then output;
if reverse(trim(STATEMENT)) =:'/*' then COMMENT=0;
keep STATEMENT;
run;
dlm=';'
makes no difference.
data _null_;
input STATEMENT $80.;
file "%sysfunc(pathname(work))\t.txt";
put STATEMENT;
cards4;
Libname sample1 "XYZ";
Libname sample2 "ABC";
/*Libname sample3 "asdfghjj";*/
Libname sample4 "uxcvdgh";
/*commented out
Libname sample5 "XYZasdf";
Libname sample6 "XYZasdfgb";**/
Libname sample7 "XYZdefrgevsdc";
;;;;
run;
data WANT;
infile "%sysfunc(pathname(work))\t.txt" pad dlm=';';
input STATEMENT $80.;
if STATEMENT =:'/*' then COMMENT+1;
if not COMMENT then output;
if reverse(trim(STATEMENT)) =:'/*' then COMMENT=0;
keep STATEMENT;
run;
NOTE: The data set WORK.WANT has 4 observations and 1 variables.
I don't understand.
Hi @ChrisNZ i have a delimiter set while reading the libname statements into a dataset.
i.e., the dlm is set in first data step itself not in WANT Data step.
thanks
That's what I do in my second example, isn't it?
I use dlm=';' to read the data from an external file into a data set.
I don't know if that's possible, at least not without some modifications to the source file, although I'd be interested to be proven wrong. (Edited to Add: Proven wrong before I even finished posting, that's gotta be a record! Interesting code @ChrisNZ, I'll have to remember that.)
I think the bigger question is why do you need to import your SAS code into a dataset? What are you actually trying to accomplish? If my assumption is correct and it's something other than just having a dataset containing parts of your SAS code, there may be a better way to go about it.
hi @Sven111 i have multiple files where mt libnames are declared.i want to store all of them in one single dataset to keep track of the locations
Are they ones you use on a regular basis or more long-term ones that you just want to track? If it's the former you could just declare them once in your SAS autoexec file so they'll be setup for whatever code your running, I have several LIBNAMES setup like that in my autoexec so I can easily reference them from EntGuide or base SAS. If it's for long-term tracking I still think a dataset isn't necessarily the best way to go, honestly I think just maintaining them in one or more text files may be better and more flexible. You could even source them using an %INCLUDE statement later on if you wanted to call them again. It's not as automated, but unless you're dealing with hundreds or thousands of LIBNAMES it's probably the way I'd go.
Plus while @ChrisNZ's code handles the examples you've provided very well, as it stands I doesn't look like it will be able to detect comments if the comment delimiters aren't at the beginning and end of the line (excluding whitespace) or detect the single line comment style. This may or may not be a problem depending on the coding style of the files you're trying to import, and ChrisNZ's code could probably be modified without much difficulty to take in most of the edge cases, but personally I think solving this a different way would be preferable. Just my $0.02.
The simplest method - and bearing in mind you need to evaluate multiple assignments and such like for instance in one file abc might be c:\dir1, in another program it might be c:\dir2, so an automatic approach might not work - would be to take a copy of SASHELP.VLIBNAME, run the programs i.e. %include, then just take a list of SASHELP.VLIBNAME not in your copy:
E.g:
data temp; set sashelp.vlibnam; run; libname abc "s:\temp\rob"; proc sql; create table WANT as select * from SASHELP.VLIBNAM except select * from TEMP; quit; libname abc clear;
This will create want with one row for the libname I just created with name and path. Far simpler than re-implementing a text parser to work out what is what - note it wont pick up on duplicates however!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.