I need to run my SAS code two time to get the macro variable value of &cyear.
How to solve that issue ?
Please note that the complete fname look like: fname=pilote_2022_2025_validation_v001.mdf.0.0.0.spds9
%let fname=pilote_????_????_validation_v001.*.spds9;
%let path2=/dwh_operation/.../pilot/&fname.;
filename oscmd pipe "ls &path2. -t | head -n1 2>&1";
data _null_;
infile oscmd;
input;
a= _infile_;
call symputx("a", a);
%let cyear=%sysfunc(substr(&a.,65,4));
run;
%put &=cyear;
How about something like this:
data _null_;
infile oscmd;
input;
a= _infile_;
call symputx("cyear", substr(a,65,4));
run;
I think it's solved when you put the %LET statement after the data step.
run;
%let cyear=%sysfunc(substr(&a.,65,4));
%put &=cyear;
Ciao,
Koen
How about something like this:
data _null_;
infile oscmd;
input;
a= _infile_;
call symputx("cyear", substr(a,65,4));
run;
The macro processor operates on the TEXT of your program first and then passes the result to SAS to execute.
SInce the %LET is BEFORE the RUN statement it executes BEFORE the data step executes. So you are running this program:
%let fname=pilote_????_????_validation_v001.*.spds9;
%let path2=/dwh_operation/.../pilot/&fname.;
filename oscmd pipe "ls &path2. -t | head -n1 2>&1";
%let cyear=%sysfunc(substr(&a.,65,4));
data _null_;
infile oscmd;
input;
a= _infile_;
call symputx("a", a);
run;
%put &=cyear;
Why not just ask the SAS code to make the macro variable?
data _null_;
infile oscmd;
input;
a= _infile_;
call symputx('a', a);
call symputx('cyear',substr(a,65,4));
run;
%put &=a;
%put &=cyear;
I have no idea why you think the year is always going to be in columns 65 to 68 , but if it is then why not just run this code instead? That way the code more closely matches the intent.
data _null_;
infile oscmd truncover;
input cyear $ 65-68;
call symputx('cyear',cyear);
run;
%put &=cyear;
This is why we advocate against having macro code in step (DATA or PROC) code.
Had you put the %LET before the DATA step (illustrating the actual sequence of execution), the mistake would have been obvious. Had you put it after the RUN, it would simply have worked as intended.
BTW you overcomplicate it. If the goal is to get cyear, this is sufficient:
data _null_;
infile oscmd;
input;
call symputx("cyear",substr(_infile_,65,4));
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.