Date: Fri, 18 May 2007 08:53:59 -0500
Reply-To: "Paul A. Thompson" <paul@WUBIOS.WUSTL.EDU>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Paul A. Thompson" <paul@WUBIOS.WUSTL.EDU>
Subject: Re: Generating %LET statements using PUT statements
In-Reply-To: <000b01c798dd$d5d81110$ca1c684a@lorne7dc2bde4c>
Content-Type: text/plain; charset="us-ascii"
Over the last 8 years or so, I have written a data system for managing
transactions using SAS/IntrNet. The system uses a macro-driven interface.
Every dataset has a set up involving macro variables for formats, variable
names, file names, etc. In that way, it is somewhat similar to what you
describe. I don't go to this trouble of writing PUT statements to write LET
statements - what I termed "second-order indirection" before. Rather, I
define macro variables which contain LET statements, so that I can simply
"unpack" and assign using a single macro variable. Thus, I say:
%global _a _b _c;
%macro _seta;
%let _a=1;
%let _b=2;
%let _c=3;
%mend _seta;
%macro _setb;
%let _a=3;
%let _b=2;
%let _c=4;
%mend _setb;
%_seta;
DO one thing
%_setb;
Do another thing.
I can even say
%let _swtch=a;
%_set&_swtch;
Paul A. Thompson, Ph.D.
Division of Biostatistics, Washington University School of Medicine
660 S. Euclid, St. Louis, MO 63110-1093
314-747-3793
paul@wubios.wustl.edu
-----Original Message-----
From: Lorne Klassen [mailto:lk1@rogers.com]
Sent: Thursday, May 17, 2007 6:48 PM
To: Paul A. Thompson
Subject: Re: Generating %LET statements using PUT statements
1) As I said, there are reasons why I write to a file first. It is fairly
complicated and hard to explain. Basically, a given set of %LET statements
is required in multiple places in the overall generated program. I only want
to generate each set of %LET statements only once for efficiency reasons. I
associate each set with an ID. Once I know that I've generated a file once,
I'll not generate it again. I'll just go get the approriate existing file of
PUT statements. It also has to do with consistency, as this is the way it is
done for other parts of the system. Plus other reasons.
2) This is the responsibility of the user to use %STR(%") instead of just "
in the VALUE column of the data set. But if the user just enters ", then
this is what I want to generate, and the program can crash. My point is that
I want to generate %LET statements with values exactly as shown in the VALUE
column.
3) You think I can get this far and not be familiar with CALL SYMPUT?? The
same problem exists with quotes in CALL SYMPUT. For example...
If my value is xxx'yyy then CALL SYMPUT('macvar','xxx'yyy') is obviously not
going to work.
If my value is xxx"yyy then CALL SYMPUT('macvar',"xxx"yyy") is obviously not
going to work.
Maybe generating %NRBQUOTE in the quotes in the second argument would help,
I'd have to try.
----- Original Message -----
From: "Paul A. Thompson" <paul@wubios.wustl.edu>
To: "'Lorne Klassen'" <lk1@ROGERS.COM>
Sent: Thursday, May 17, 2007 5:22 PM
Subject: RE: Generating %LET statements using PUT statements
> Gzsz! I thought I was convoluted.
>
> Several points:
> 1) Why are you doing second-order indirection? Why not just write the
> %let
> statements, instead of writing a file which writes the %let statements?\
>
> 2) You have a macro with an unclosed ". This will cause a buncha
> problems.
> Better quote this.
>
> 3) Finally, you should learn about the CALL SYMPUT statement, which is
> designed to safely write wacky stuff into macro variables.
>
> Paul A. Thompson, Ph.D.
> Division of Biostatistics, Washington University School of Medicine
> 660 S. Euclid, St. Louis, MO 63110-1093
> 314-747-3793
> paul@wubios.wustl.edu
>
>
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Lorne
> Klassen
> Sent: Thursday, May 17, 2007 4:11 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Generating %LET statements using PUT statements
>
> I'm trying to generate a text file of PUT statements, each containing a %
> LET statement. This text file can then be %INCLUDED in a DATA _NULL_ step
> to write the %LET statements to a final text (.sas) file. I won't get into
> the details of why it has to be done this way, but it does.
>
> Here is the input data set:
>
> q5_2007_MACVARS
> ---------------
> MACVAR VALUE
> ------ -----
> C10 37%STR(%')3
> C11 23'432
> C12
> C13 EE"E
> C14 &abc
> C15 %eee
>
> Here is the data step that creates a text file of PUT statements, each
> containing a %LET statement:
>
> DATA _NULL_;
> SET q5_2007_MACVARS;
> FILE "c:\test.txt" LRECL=2000;
> PUT "PUT '%" "LET " MACVAR "= " VALUE +(-1) ";';";
> RUN;
>
> Here is the resulting text file:
>
> c:\test.txt
> -----------
> PUT '%LET C10 = 37%STR(%')3;';
> PUT '%LET C11 = 23'432;';
> PUT '%LET C12 = ;';
> PUT '%LET C13 = EE"E;';
> PUT '%LET C14 = &abc;';
> PUT '%LET C15 = %eee;';
>
> Here is the data step that reads the test.txt file and creates final.sas
> which has the %LET statements:
>
> DATA _NULL_;
> FILE "c:\final.sas" LRECL=2000;
> %INCLUDE "c:\test.txt";
> RUN;
>
> Finally, here is the generated program file final.sas. Actually, this is
> what I want, but I'm having problems getting this.
>
> c:\final.sas
> ------------
> %LET C10 = 37%STR(%')3;
> %LET C11 = 23'432;
> %LET C12 = ;
> %LET C13 = EE"E;
> %LET C14 = &abc;
> %LET C15 = %eee;
>
> The problem is that the input data set can have *any characters* in the
> VALUE column. This includes single quotes, double quotes, ampersands and
> percent signs, %STR, %NRSTR, etc.
>
> The file test.txt is created as shown, with no problem. The problem is the
> data step that creates final.sas. My data has single quotes, double
> quotes,
> etc. The last data step fails because I have single quotes around my PUT
> statements (and single quotes in these quotes). I've tried %NRBQUOTE and %
> NRSTR in various places but I can't get exactly what is contained in the
> VALUE column to end up in final.sas. I either end up with an error because
> I have single quotes inside of single quotes, or double quotes inside of
> double quotes, or %STR gets stripped off too early, etc.
>
|