[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sas-l
Subject:    Why use CHAR instead of SUBSTR for single character substrings
From:       Roger DeAngelis <rogerjdeangelis () GMAIL ! COM>
Date:       2016-03-31 16:49:55
Message-ID: 3346651946891996.WA.rogerjdeangelisgmail.com () listserv ! uga ! edu
[Download RAW message or body]

Why use CHAR instead of SUBSTR for single character substrings


I lost the link
but this is the solution on the SAS forum by data _null_


HAVE

data abc;
 retain str 'ABC';
output;
;run;quit;

/*
Up to 40 obs WORK.ABC total obs=1

     Type  Length   Value
STR    C    3       ABC


Obs    STR

 1     ABC


WANT  (minimal code to create tre with
      length 1)

           Length

TRE    C    1

Up to 40 obs WORK.TRE total obs=3

Obs    TRE

 1      A
 2      B
 3      C

*/

PROBLEM

data tre_wrong+length;
  set abc;
  do i=1 to lengthn(str);
    tre=substr(str,i,1);
    output;
  end;
  drop i str;
;run;quit;

TRE    C    3

Solution


data tre;
  set abc;
  do i=1 to lengthn(str);
    tre=char(str,i);
    output;
  end;
  drop i str;
;run;quit;

/*
Middle Observation(1 ) of Last dataset = WORK.TRE - Total Obs 3

 -- CHARACTER --
TRE                C    1       A
*/
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic