sas

How the TRIM function works in this example?


I don't understand why the asterisk won't be placed after 92 and 110, if the TRIM function is excluded or the TRIM function is moved outside of the || function. The DBP_CHK OUTPUT in the SAS LOG before being applied the concatenation on

DATA EXAMPLE8;
      INPUT SBP DBP @@;
      LENGTH SBP_CHK DBP_CHK $ 4;
      SBP_CHK = PUT(SBP,3.);
      DBP_CHK = PUT(DBP,3.);
      LDBP_CHK = LENGTHC(DBP_CHK);
      IF SBP GT 160 THEN SBP_CHK = SUBSTR(SBP_CHK,1,3) || '*'; 
      IF DBP GT 90 THEN DBP_CHK = TRIM(DBP_CHK) || '*'; 
      put DBP_CHK $quote.;
      DATALINES;
      120 80 180 92 200 110
      ;
      PROC PRINT DATA=EXAMPLE8 NOOBS;
      TITLE "Listing of Example 8";
      RUN;
If I do DBP_CHK = TRIM(DBP_CHK); before the IF-THEN statement, and then just have DBP_CHK = DBP_CHK|| '*', it won't work out.

Solution

  • In SAS DATA Step the value of a character variable is padded with spaces to the length of the variable.

    Suppose you have code:

    LENGTH DBP_CHK $ 4;
    DBP_CHK = '1' ;
    DBP_CHK = DBP_CHK || '*';
    

    The concatenated result is $5 ( 1 * ) that is forced into a $4 container (DBP_CHK) and thus truncation of the result occurs, and you scratch your head.