Search code examples
sastrimproc-sql

SAS Proc SQL Trim not working?


I have a problem that seems pretty simple (probably is...) but I can't get it to work.

The variable 'name' in the dataset 'list' has a length of 20. I wish to conditionally select values into a macro variable, but often the desired value is less than the assigned length. This leaves trailing blanks at the end, which I cannot have as they disrupt future calls of the macro variable.

I've tried trim, compress, btrim, left(trim, and other solutions but nothing seems to give me what I want (which is 'Joe' with no blanks). This seems like it should be easier than it is..... Help.

data list;
    length id 8 name $20;
    input id name $;
cards;
1 reallylongname
2 Joe
;
run;

proc sql;
    select trim(name) into :nameselected
    from list
    where id=2;
run;

%put ....&nameselected....;

Solution

  • Actually, there is an option, TRIMMED, to do what you want.

    proc sql noprint;
        select name into :nameselected TRIMMED
        from list
        where id=2;
    quit;
    

    Also, end PROC SQL with QUIT;, not RUN;.