BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

How about something like this:

 

data _null_;
    infile oscmd;
    input;
    a= _infile_;
    call symputx("cyear", substr(a,65,4));    
run;
--
Paige Miller

View solution in original post

5 REPLIES 5
sbxkoenk
SAS Super FREQ

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

PaigeMiller
Diamond | Level 26

How about something like this:

 

data _null_;
    infile oscmd;
    input;
    a= _infile_;
    call symputx("cyear", substr(a,65,4));    
run;
--
Paige Miller
Tom
Super User Tom
Super User

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;

 

Kurt_Bremser
Super User

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 263 views
  • 3 likes
  • 5 in conversation