pyll
pyll

Reputation: 1754

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....;

Upvotes: 4

Views: 3144

Answers (2)

DomPazz
DomPazz

Reputation: 12465

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;.

Upvotes: 14

Jeff
Jeff

Reputation: 1807

It works if you specify a separator:

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

Upvotes: 6

Related Questions