Character Functions in SAS

download Character Functions in SAS

of 9

Transcript of Character Functions in SAS

  • 8/17/2019 Character Functions in SAS

    1/9

    Program 1:

    data storage;

    length A $ 4 B $ 4;

    Name = 'Goldstein';

    AandB = A || B;Cat = cats(A,B);

    if Name = 'Smith' then atch = 'No';

    else atch = '!es';

    S"#string = s"#str(Name,,%);

    &A = length(A);

    &B = length(B);

    &Name = lengthc(Name);

    &AandB = lengthc(AandB);

    &Cat = lengthc(Cat);

    &atch = lengthc(atch);&S"string = lengthc(S"#string);

    r"n;

    roc rint data=storage;

    ar &* ;

    r"n;

    o+*

    Obs L_A L_B L_Name L_AandB L_Cat L_Match L_Sustring

    1 1 1 9 8 200 2 9

    Program 2:

    data i;

    set '+home+-ra-ee.4/+sas"ser0.4+mied0sas1#dat';

    Name&o2 = lo2case(Name);

    Namero = rocase(Name);

    +3 method to conert Name in roer case 2itho"t "sing the 56CAS7 f"nction

    3+

    8irst = lo2case(scan(Name,9,' ')); +3 conerting :rst into lo2er case 3+

    &ast = lo2case(scan(Name,%,' ')); +3 conerting last into lo2er case 3+

    s"#str(8irst,9,9) = "case(s"#str(8irst,9,9)); +3 conerting :rst into "er case 3+

    s"#str(&ast,9,9) = "case(s"#str(&ast,9,9)); +3 conerting last into "er case 3+

    3"sing cat maing :rst letter of each 2or into "ercase,2itho"t "sing

    rocase;

    Name

  • 8/17/2019 Character Functions in SAS

    2/9

    Obs Name ID NameLow Nameprop NameHard

    2 Patrice Helms 233 patrice helms Patrice Helms Patrice Helms

    Thomas chien 998 thomas chien Thomas Chien Thomas Chien

    Program 3:

    data namesandmore;

    set '+home+-ra-ee.4/+sas"ser0.4+namesandmore0sas1#dat';

    Name = com#l(Name);

    hone = comress(hone,,'d');

    r"n;

    title &isting of NA7SAN>657;

    roc rint data=namesandmore noo#s;

    r"n;o+*

    Listing o" NAM#S_AND_MO$#

    Name %hone Height Mi&ed

    Roger Cody 9087821234 5t! 10in! 50 1"8

    Thomas #eerson 3158488484 $t! 1in! 23 1"2

    %arco Polo 80012345$7 5&t! $in! 40

    'rian (atson 51835517$$ 5t! 10in 89 3"4

    %ichael )e%arco 4452322233 $t! 7$ 1"3

    Program 4:

    li#name ?li# '+home+-ra-ee.4/+sas"ser0.4';

    data height;

    set ?li#0namesandmore(ee=

  • 8/17/2019 Character Functions in SAS

    3/9

    Obs Height HtInches

    1 5 10 70

    2 $ 1 73

    5 $ $$

    ' 5 10 70

    ( $ 72

    Program 5:

    li#name ?li# '+home+-ra-ee.4/+sas"ser0.4';

    data conert;set ?li#0namesandmore(ee= ied);

    @nteger = in"t(scan(ied,9, ' +'),0);

    N"merator = in"t(scan(ied,%,' +'),0);

    >enominator = in"t(scan(ied,E,' +'),0);

    if missing(N"merator) then rice = @nteger;

    else rice = @nteger D N"merator+>enominator;

    dro @nteger N"merator >enominator;

    r"n;

    roc rint data=conert;

    r"n;

    o+*

    Obs Mi&ed %rice

    1 50 1"8 50!1250

    2 23 1"2 23!5000

    40 40!0000

    ' 89 3"4 89!7500

    ( 7$ 1"3 7$!3333

    Program 6:

    data St"d?;

    set '+home+-ra-ee.4/+sas"ser0.4+st"d?0sas1#dat';

    length Gro">ose $F;

    Gro">ose = cat('',Gro",>ose);

    +3 2itho"t "sing cat f"nction*

    Gro">ose = trim(Gro") || HH || >ose; 3+dro Gro" >ose;

  • 8/17/2019 Character Functions in SAS

    4/9

    r"n;

    roc rint data=St"d? noo#s;

    r"n;

    o+*

    Sub) *eight Subgroup +roupDose

    001 220l*s! 2 +,-o.

    002 90/g! 1 +,High

    003 88g 1 ',-o.

    004 1$5l*s! 2 ',High

    005 88 1 +,-o.

    Program 7:

    data Gro"S"#gro";

    set '+home+-ra-ee.4/+sas"ser0.4+st"d?0sas1#dat';

    length Com#ined $E;

    3Com#ined = trim(Gro") ||''|| "t(S"#gro",90);

    Com#ined = cat('',Gro",S"#gro");

    r"n;

    roc rint data=Gro"S"#gro" noo#s;

    r"n;

    o+*

    Sub) +roup Dose *eight Subgroup Combined

    001 + -o. 220l*s! 2 +,2

    002 + High 90/g! 1 +,1

    003 ' -o. 88g 1 ',1

    004 ' High 1$5l*s! 2 ',2

    005 + -o. 88 1 +,1

    Program 8:

    data St"d?Ieight;

    set '+home+-ra-ee.4/+sas"ser0.4+st"d?0sas1#dat';

    3"sing comress(d)to ee n"merical al"es alone and change if character

    aria#les resent to n"merical;

    Ieight = in"t(comress(Ieight,,'d'),0);

    3"sing :nd f"nction 2ith i arg"ment to remoe characters and to ignore cases;if :nd(Ieight,'JG','i') then Ieight = ro"nd(%0%3Ieight,09);

  • 8/17/2019 Character Functions in SAS

    5/9

    else if :nd(Ieight,'&B','i') then Ieight = ro"nd(Ieight,09);

    r"n;

    roc rint data=St"d?Ieight noo#s;

    r"n;

    o+*

    Sub) +roup Dose *eight Subgroup

    001 + -o. 220 2

    002 + High 90 1

    003 ' -o. 88 1

    004 ' High 1$5 2

    005 + -o. 88 1

    Program 9:

    data sirited;

    set '+home+-ra-ee.4/+sas"ser0.4+sales0sas1#dat';

    2here :nd(C"stomer,'sirit','i');

    r"n;

    roc rint data=sirited noo#s;

    r"n;

    o+*

    #mpID Name $egion Customer Item ,uantit- .nitCost /ota0Sa0es

    0017 #ason ygen ast Rogers 6pirits 122- 500 39!99 19995

    0017 #ason ygen 6oth 6pirited 6pirits 407 100 19!95 1995

    Program 10:

    data 7rrors;

    set '+home+-ra-ee.4/+sas"ser0.4+errors0sas1#dat';

    2here comress(S"#-,,'d') or comress(artN"m#er,'&S','d');+32here notdigit(trim(S"#-)) or erif?(trim(artN"m#er),'/9%E4F1.&S'); 3+

    r"n;

    roc rint data=7rrors noo#s;

    r"n;

    o+*

    Sub) %artNumber Name

    0a2 -887 &red 'eans

    004 a*cde %ary 'mpers

  • 8/17/2019 Character Functions in SAS

    6/9

    Sub) %artNumber Name

    89 88886 ill 6andord

    Program 11:data 7rrors;

    set '+home+-ra-ee.4/+sas"ser0.4+errors0sas1#dat';

    2here an?digit(Name);

    r"n;

    roc rint data=7rrors noo#s;

    ar S"#- Name;

    r"n;

    o+*

    Sub) Name

    003 +lred 2 ice

    Program 12:

    data 7rrors;

    set '+home+-ra-ee.4/+sas"ser0.4+errors0sas1#dat';

    r"n;

    roc rint data=7rrors noo#s;

    2here :ndc(artN"m#er,'K>','i');

    ar S"#- artN"m#er;r"n;

    o+*

    Sub) %artNumber  

    0a2 -887

    004 a*cde

    Program 13:

    data eact 2ithin%;

    set '+home+-ra-ee.4/+sas"ser0.4+social0sas1#dat';

    if SS9 eL SS% then o"t"t eact;

    else if sedis(SS9,SS%) le % then o"t"t 2ithin%;

    r"n;

    title &isting of 7KAC;

    roc rint data=eact noo#s;

    r"n;

    title &isting of I@

  • 8/17/2019 Character Functions in SAS

    7/9

    o+*

    Listing o" #AC/

    SS1 SS2

    123,45,$789 123,45,$789

    007,77,$7$7 007,77,$7$7

    Listing o" *I/HIN2(

    SS1 SS2

    001,34,987$ 001,43,987$

    102,43,9182 102,43,9188

    Program 14:

    data edical;

    set '+home+-ra-ee.4/+sas"ser0.4+medical0sas1#dat';

    r"n;

    roc rint data=edical noo#s;

    2here :nd2(Comment,'anti#iotics');

    ar atno Comment;

    r"n;

    o+*

    %atno Comment

    002 Patient has *een on anti*iotics or 10 days

    Program 15:

    li#name ?li# '+home+-ra-ee.4/+sas"ser0.4';

    data n"m#ers;set ?li#0namesandmore(ee= hone);

    length Areacode $E;

    Areacode = s"#str(hone,%,E);

    r"n;

    roc rint data=n"m#ers;

    r"n;

    o+*

    Obs %hone Areacode

    1 908782,1234 908

  • 8/17/2019 Character Functions in SAS

    8/9

    Obs %hone Areacode

    2 315 848,8484 315

    800123,45$7 800

    ' 518355,17$$ 518

    ( 445232,2233 445

    Program 16:

    li#name ?li# '+home+-ra-ee.4/+sas"ser0.4';

    data namelist;

    set ?li#0namesandmore(ee= Name);

    length &astname $9;Name = com#l(Name);

    &astname = scan(Name,%,' ');

    r"n;

    roc sort data=namelist;

    #? &astname;

    roc rint data=namelist;

    r"n;

    o+*

    Obs Name Lastname

    1 Roger Cody Cody

    2 %ichael )e%arco )e%arco

    Thomas #eerson #eerson

    ' %arco Polo Polo

    ( 'rian (atson (atson

    Program 17:

    li#name ?li# '+home+-ra-ee.4/+sas"ser0.4';

    data ersonal;

    set ?li#0ersonal (dro= 8ood98ood);

    s"#str(SS,9,1) = '333333';

    s"#str(AcctN"m,,9) = '';

    r"n;

    roc rint data=ersonal;

    r"n;

    o+*

  • 8/17/2019 Character Functions in SAS

    9/9

    Obs SS +ender AcctNum DOB

    1 :::::: $789 % 0192, 11"15"1949

    2 :::::: 9388 & 9981, 01"02"1981

    :::::: 1309 % 1322, 03"29"1988

    ' :::::: 4$55 & 9899, 07"04"1981

    ( :::::: 4455 % 2938, 08"09"1977