Hi,
I'm working on SAS 94.
My final goal is to put the following file path into a macro variable:
%LET sdtm_bckup_path =
%NRSTR(%RXC_SAS_DATA%)&DL.%NRSTR(&UNIXDIR.)&DL.%NRSTR(&mrocf.)&DL.%NRSTR(&sdtm.)&DL.&dttm;
As you can see, it's populated by % signs and & signs.
I tried to use both %NRSTR and %NRBQUOTE; the first function works in case "%RXC_SAS_DATA" with only one % sign.
Any idea how to handle "%RXC_SAS_DATA%" or just "%" ?
Thanks in advance
Hi,
When specifying:
X "mkdir ""&sdtm_bckup_path""";
Should you be using the following instead:
X "mkdir ""&work_bckup_path""";
If not, then please post the full log using the insert code icon "</>". Does it show the bckup_w library was allocated successfully?
Thanks & kind regards,
Amir.
Hi @OGA13,
Have you tried something like (double % signs):
%let var = %nrstr(%%text%%);
%put &=var;
for which the log shows:
72 %let var = %nrstr(%%text%%); 73 %put &=var; VAR=%text% 74
If the above does not help then what resulting string do you want to see when you reference macro variable sdtm_bckup_path? I'm unclear if you want any ampersands ("&") in the final string.
Thanks & kind regards,
Amir.
/* Create a Datetime macro variable */
%LET dttm = %SYSFUNC( DATE(), yymmdd10. )T%SYSFUNC( TRANSLATE( %SYSFUNC( TIME(), tod8.), %STR(-),%STR(:) ) );
%LET sdtm_path = %NRSTR(%RXC_SAS_DATA%%)&DL.&UNIXDIR.&DL.&mrocf.&DL.&sdtm.;
i'm sharing the full macro:
/* Create a new <SDTM> sub-directory */
%LET sdtm_bckup_path = %NRSTR(%RXC_SAS_DATA%%)&DL.&UNIXDIR.&DL.&mrocf.&DL.&sdtm.&DL.&dttm;
*SET OPTIONS XWAIT=ON;
/* Create the backup directory */
X "mkdir ""&sdtm_bckup_path""";
*X "echo ""&sdtm_bckup_path""";
/* Copy files from the SDTM path to the backup path*/
X "copy ""&sdtm_path&DL.*.*"" ""&sdtm_bckup_path""";
Double %% sign did actually solved the problem. To make it more clear, i'm sending the string to the command line; therefore i want the ampersand to be resolved.
I tried to remove the unneeded %nrstr() function, but it still seems to fail.
Hi,
Please respond to the following:
Thanks & kind regards,
Amir.
1. That's the desired path:
%LET sdtm_path = %NRSTR(%RXC_SAS_DATA%%)&DL.&UNIXDIR.&DL.&mrocf.&DL.&sdtm.; %put &sdtm_path; %RXC_SAS_DATA%\unixdir\mrocf\SDTM
2. The actual error is given by another macro which uses the macro.
/* 4) Backup WORK folder*/ %LET work_bckup_path =%NRSTR(%RXC_SAS_DATA%%)&DL.&UNIXDIR.&DL.&mrocf.&DL.wbckup ; X "mkdir ""&sdtm_bckup_path"""; LIBNAME bckup_w "&work_bckup_path" ; PROC DATASETS LIB=bckup_w KILL NOLIST MEMTYPE=data ; QUIT ; PROC DATASETS ; COPY IN = work OUT = bckup_w memtype= data ; RUN ; QUIT ; %logmsg( level = I, function = HOR_START, message = %STR( Backup WORK folder ), action = C ) ; INFO: (HOR_START) - Backup options including SASAUTOS %RXC_SAS_DATA%\unixdir\mrocf\mapping\bckup INFO: (HOR_START) - Backup SDTM folder WARNING: Library BCKUP_W does not exist. ERROR: Library BCKUP_W does not exist.
Hi,
When specifying:
X "mkdir ""&sdtm_bckup_path""";
Should you be using the following instead:
X "mkdir ""&work_bckup_path""";
If not, then please post the full log using the insert code icon "</>". Does it show the bckup_w library was allocated successfully?
Thanks & kind regards,
Amir.
A couple of other points.
You can use single quotes around a value to prevent the macro processor from evaluating the macro triggers (% and &). You can then use %qsysfunc(dequote()) to get the value out of quotes and apply macro quoting.
%let path=%qsysfunc(dequote('%ENVAR%\R&D Department\'));
In general it is better to use a PIPE to run operating system commands. That way you can see the error messages.
data _null_;
infile "mkdir ""&sdtm_bckup_path""" pipe;
input;
put _infile_;
run;
Which means you can then use that data step to manipulate your strings and not have to worry about the macro processor trying to evaluate the value of the macro variable.
data _null_;
length cmd $400;
cmd = catx(' ','mkdir',quote(trim(symget('sdtm_bckup_path'))));
infile cmd pipe filevar=cmd ;
input;
put _infile_;
run;
I can possibly see why you might need the % characters in that string since it looks like you might be running on Windows and what to reference an environment variable using the %NAME% syntax of Windows/DOS operating system.
But why would you want to also include & characters? Do your directory names actual have & characters in them? It does not look like that is what you are trying to describe. Instead it just looks like you are building a string by expanding a lot of already defined macro variables.
First thing I would do is move the %RXC_SAS_DATA% to its own macro variable so its value can be macro quoted.
%let topdir=%str(%%RXC_SAS_DATA%%);
Then you can build the desired path using something like:
%let topdir=%str(%%RXC_SAS_DATA%%);
%let DL = \ ;
%let UNIXDIR=fred ;
%let mrocf=XXX;
%let sdtm=AE ;
%let DTTM=202410180759;
%LET sdtm_bckup_path = &topdir&dl&unixdir&dl&mrocf&dl&sdtm&dl&dttm;
And if you want to get the value of an environment variable you can use SYSGET or %SYSGET.
%let topdir=%sysget(RXC_SAS_DATA);
If you need it macro quoted use %QSYSFUNC() to call the actual SYSGET() function.
%let topdir=%qsysfunc(sysget(RXC_SAS_DATA));
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.