Search code examples
sasmacroscharacter

SAS remove spaces and last 3 characters of a macro variable


I'm a new SAS user and I'm working on making some code modular. I want to create a macro variable to reference any county name that's formatted within the data, and a second macro variable that removes spaces from the first macro variable to create a resolved macro to be used in a dataset name. Here's where I am:

options symbolgen;
%let county_formatted=CONTRA COSTA CO.;
%let county=%sysfunc(substr(%sysfunc(compress(&county_formatted)), 1, %sysfunc(length(%sysfunc(&county_formatted)))-3))));
run;
options nosymbolgen;

Within the dataset, county names are formatted to look like "CONTRA COSTA CO.". I'd like to take any county name and compress it for the purpose of creating output datasets with the macro resolving to "contracosta" or "CONTRACOSTA". All values for county_formatted have "CO." as the last three characters, and some county names have more than one word, such as "CONTRA COSTA CO.", "SAN LUIS OBISPO CO.", or "SHASTA CO.". I want to land on "CONTRACOSTA", "SANLUISOBISPO", or "SHASTA" for the resolved value of &county.

From the above code, I get the following error: ERROR: Expected close parenthesis after macro function invocation not found.

I tried building from the inside out and was able to compress CONTRA COSTA CO. as needed to CONTRACOSTACO. but can't seem to remove the last 3 characters.

I'd appreciate any help in correcting my code.

Thanks


Solution

  • This will works:

    %let county_formatted=CONTRA COSTA CO.;
    %let county_compressed=%sysfunc(compress(&county_formatted.));
    %let county=%sysfunc(substr(&county_compressed., 1, %length(&county_compressed.)-3));
    %put ***&county.***;
    

    Your question comes from %sysfunc(length(%sysfunc(&county_formatted)))-3), the length() function returns length of raw string, it is with space.
    By the way, SAS has %length(), the macro version of %sysfunc(length()).