简体   繁体   中英

SAS variable with blank spaces

I am trying this statement:

 CASE WHEN SUBSTR(NUMERO_TELEFONO,1,2) ='34' OR  SUBSTR(NUMERO_TELEFONO,1,3) 
    ='+34' THEN '+34'
    ELSE '     ' END

but whey I run the code, the else statment doesn´t keep the blank spaces. How can I create a variable that contains 6 blank spaces?

In SAS character values are right padded with spaces to the length attribute of the variable.

I will presume you are using the LENGTH function to check the number of characters in the value. The LENGTH function in SAS returns the number of characters in a value up to the last space in the value. Use LENGTHC to examine the length attribute of a variable or result.

Character variables can not be empty strings with 0 characters, the expression '' is expanded to one space in the context of DATA step variable evaluations. There is no concept of a null string (such as in C) in SAS. Base SAS does not have the concept of VARCHAR found in data base systems. ( NOTE: Proc DS2 does understand VARCHAR during execution time )

Example:

data have;
  length phone $15.;
  phone = '3456789'; output;
  phone = '+3467890'; output;
  phone = '1234567'; output;
run;

proc sql;
  create table want as
  select
    case 
      when substr(phone,1,2) = '34' or substr(phone,1,3) = '+34' 
      then '+34'
      else '      '
    end as code
  from 
    have
  ;

ods listing;

  describe table want;
quit;
----- LOG -----
NOTE: SQL table WORK.WANT was created like:

create table WORK.WANT( bufsize=65536 )
  (
   code char(6)
  );

Check how a string of only spaces evaluates. For the case of the blank assignment in else notice how the flag result is always 1 regardless of the number of spaces in the comparison string literal.


data check;
  set want;
  flag1 = (code = '');
  flag2 = (code = ' ');
  flag3 = (code = '  ');
  flag4 = (code = '   ');
  flag5 = (code = '    ');
  flag6 = (code = '     ');
  flag7 = (code = '      ');

  format flag: 4.;
run;

proc print data=check; title;
run;
----- OUTPUT window -----
Obs    code    flag1    flag2    flag3    flag4    flag5    flag6    flag7

 1     +34        0        0        0        0        0        0        0
 2     +34        0        0        0        0        0        0        0
 3                1        1        1        1        1        1        1

SAS character variables are fixed length and padded with spaces. You can use the LENGTH= attribute when defining the variable to set the length, there is no need to count how many spaces you type into your ELSE clause.

create table want as 
select CASE WHEN SUBSTR(NUMERO_TELEFONO,1,2) ='34'
             OR  SUBSTR(NUMERO_TELEFONO,1,3) ='+34' THEN '+34'
       ELSE ' ' 
       END as prefix length=6
from ...

My first idea:The sas dataset length define the $6 column with spaces. For example you can view the newVariableTest column in this output you will see that plus "X" append for to six " " but you dont need definie it. So you use dataset and define the result column length you are ready.

Example:

    data test;
    NUMERO_TELEFONO="343017236182";output;
    NUMERO_TELEFONO="363017236182";output;
    run;

    data test;
     set test;
     length newVariable $6;
     length newVariabletest $7;
     if SUBSTR(NUMERO_TELEFONO,1,2) ='34' OR  SUBSTR(NUMERO_TELEFONO,1,3) ='+34' 
         then newVariable='+34';
     /*for the test*/
     newVariabletest=newVariable||"X";
    run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM