0
votes

I'm having problems with getting the hang of using functions in SAS, the syntax is confusing.

I'm trying to get name without a date in the end. For example I have something like this: "MODEL_NAME_202101" and I want "MODEL_NAME".

I'm doing it like so.

%let model_ds = 
%sysfunc(
    substr(
        %scan(&models_list, 12), 
        0, 
        %length(%scan(&models_list, 12)) - 7
    )
);

%put &model_ds;

And only thing I get is a warring that tells me nothing about syntax errors I'm making.

WARNING: Argument 2 to function SUBSTR referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.

No idea how I'm supposed to nest function calls. Is every call required to have %sysfunc before call, or nesting functions inside already called function inside %sysfunc are syntax correct.

Would be nice if someone could reference me to explanation/documentation of this specific "feature",

Thanks

2
This seems partly bad design or at least not designed optimally for SAS. It's rare to need macro loops in SAS for one, BY group processing and DoW loops are typically faster, and data steps/call execute are incredibly powerful. Macro coding is more difficult in general. - Reeza
SAS did not skip kindergarten, so it knows that counting starts from 1, not 0. - Tom
@Tom Counting numbers is usually taught in pre-algebra; source: my child just learned them in a pre-algebra workbook. - Joe

2 Answers

0
votes

Rhythm is likely correct about the best solution for you - but the specific error:

WARNING: Argument 2 to function SUBSTR referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.

That's because of this:

substr(...,0,...)

That second argument is "what position to start from". SAS is 1-based, not 0-based (are you a python developer originally? This may be a bit challenging then!), so it needs to never be lower than 1.

substr(...,1,...)
0
votes

Why are you using %scan function here? Also, there is %substr, so need to use %sysfunc. See code below:

  %let models_list=MODEL_NAME_202101;
  %let model_ds = %substr(&models_list,1,%eval(%length(&models_list)-7));
  %put &model_ds;

Is this what you are looking for?