0% found this document useful (0 votes)
71 views

Character Functions in SAS

The programs demonstrate various string manipulation and data validation techniques in SAS including: 1) Concatenating, extracting, and modifying substrings within variables 2) Converting between character and numeric data types 3) Validating data for expected formats and values 4) Filtering and sorting data based on string matching and comparisons The programs read in external data, perform string/data processing, and output results for analysis and validation.

Uploaded by

vam_1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Character Functions in SAS

The programs demonstrate various string manipulation and data validation techniques in SAS including: 1) Concatenating, extracting, and modifying substrings within variables 2) Converting between character and numeric data types 3) Validating data for expected formats and values 4) Filtering and sorting data based on string matching and comparisons The programs read in external data, perform string/data processing, and output results for analysis and validation.

Uploaded by

vam_1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Program 1:

data storage;
length A $ 4 B $ 4;
Name = 'Goldstein';
AandB = A || B;
Cat = cats(A,B);
if Name = 'Smith' then Match = 'No';
else Match = 'Yes';
Substring = substr(Name,5,2);
L_A = length(A);
L_B = length(B);
L_Name = lengthc(Name);
L_AandB = lengthc(AandB);
L_Cat = lengthc(Cat);
L_Match = lengthc(Match);
L_Sustring = lengthc(Substring);
run;
proc print data=storage;
var L_: ;
run;
o/p:
Obs

L_A

L_B

L_Name

L_AandB

L_Cat

L_Match

L_Sustring

200

Program 2:
data Mix;
set '/home/jrajeev940/sasuser.v94/mixed.sas7bdat';
NameLow = lowcase(Name);
Nameprop = propcase(Name);
/* method to convert Name in proper case without using the PROPCASE function
*/
First = lowcase(scan(Name,1,' ')); /* converting first into lower case */
Last = lowcase(scan(Name,2,' ')); /* converting last into lower case */
substr(First,1,1) = upcase(substr(First,1,1)); /* converting first into upper case */
substr(Last,1,1) = upcase(substr(Last,1,1)); /* converting last into upper case */
*using catx making first letter of each work into uppercase,without using
propcase;
NameHard = catx(' ',First,Last);
drop First Last;
run;
proc print data=Mix;
run;
o/p:
Obs Name

ID NameLow

Nameprop

NameHard

Obs Name

ID NameLow

Nameprop

NameHard

2 Patrice Helms

233 patrice helms

Patrice Helms

Patrice Helms

3 Thomas chien

998 thomas chien

Thomas Chien

Thomas Chien

Program 3:
data names_and_more;
set '/home/jrajeev940/sasuser.v94/names_and_more.sas7bdat';
Name = compbl(Name);
Phone = compress(Phone,,'kd');
run;
title "Listing of NAMES_AND_MORE";
proc print data=names_and_more noobs;
run;
o/p:

Listing of NAMES_AND_MORE
Name

Phone

Height

Mixed

Roger Cody

9087821234

5ft. 10in.

50 1/8

Thomas Jefferson

3158488484

6ft. 1in.

23 1/2

Marco Polo

8001234567

5Ft. 6in.

40

Brian Watson

5183551766

5ft. 10in

89 3/4

Michael DeMarco

4452322233

6ft.

76 1/3

Program 4:
libname Mylib '/home/jrajeev940/sasuser.v94';
data height;
set Mylib.names_and_more(keep= Height);
Height = compress(Height,'INFT.', 'i');
Feet = input(scan(Height,1,' '),8.);
Inches = input(scan(Height,2,' '),?? 8.);
if missing(Inches) then HtInches = 12*Feet;
else HtInches = 12*Feet + Inches;
drop Feet Inches;
run;
proc print data=height;
run;
o/p:

Obs Height

HtInches

1 5 10

70

2 61

73

3 56

66

4 5 10

70

5 6

72

Program 5:
libname Mylib '/home/jrajeev940/sasuser.v94';
data convert;
set Mylib.names_and_more(keep= Mixed);
Integer = input(scan(Mixed,1, ' /'),8.);
Numerator = input(scan(Mixed,2,' /'),8.);
Denominator = input(scan(Mixed,3,' /'),8.);
if missing(Numerator) then Price = Integer;
else Price = Integer + Numerator/Denominator;
drop Integer Numerator Denominator;
run;
proc print data=convert;
run;
o/p:
Obs Mixed

Price

1 50 1/8

50.1250

2 23 1/2

23.5000

3 40

40.0000

4 89 3/4

89.7500

5 76 1/3

76.3333

Program 6:
data Study;
set '/home/jrajeev940/sasuser.v94/study.sas7bdat';
length GroupDose $6;
GroupDose = catx('-',Group,Dose);
/* without using cat function:
GroupDose = trim(Group) || - || Dose; */
drop Group Dose;

run;
proc print data=Study noobs;
run;
o/p:
Subj

Weight

Subgroup GroupDose

001

220lbs.

2 A-Low

002

90Kg.

1 A-High

003

88kg

1 B-Low

004

165lbs.

2 B-High

005

88kG

1 A-Low

Program 7:
data Group_Subgroup;
set '/home/jrajeev940/sasuser.v94/study.sas7bdat';
length Combined $3;
*Combined = trim(Group) ||'-'|| put(Subgroup,1.);
Combined = catx('-',Group,Subgroup);
run;
proc print data=Group_Subgroup noobs;
run;
o/p:
Subj

Group

Dose

Weight

Subgroup Combined

001

Low

220lbs.

2 A-2

002

High

90Kg.

1 A-1

003

Low

88kg

1 B-1

004

High

165lbs.

2 B-2

005

Low

88kG

1 A-1

Program 8:
data Study_Weight;
set '/home/jrajeev940/sasuser.v94/study.sas7bdat';
*using compress(kd)to keep numerical values alone and change if character
variables present to numerical;
Weight = input(compress(Weight,,'kd'),8.);
*using find function with "i" argument to remove characters and to ignore cases;
if find(Weight,'KG','i') then Weight = round(2.2*Weight,.1);

else if find(Weight,'LB','i') then Weight = round(Weight,.1);


run;
proc print data=Study_Weight noobs;
run;
o/p:
Subj

Group

Dose

Weight

Subgroup

001

Low

220

002

High

90

003

Low

88

004

High

165

005

Low

88

Program 9:
data spirited;
set '/home/jrajeev940/sasuser.v94/sales.sas7bdat';
where find(Customer,'spirit','i');
run;
proc print data=spirited noobs;
run;
o/p:
EmpID

Name

Region

Customer

Item

Quantity

UnitCost

TotalSales

0017

Jason Nuygen

East

Roger's Spirits

122L

500

39.99

19995

0017

Jason Nuygen

South

Spirited Spirits

407XX

100

19.95

1995

Program 10:
data Errors;
set '/home/jrajeev940/sasuser.v94/errors.sas7bdat';
where compress(Subj,,'d') or compress(PartNumber,'LS','d');
/*where notdigit(trim(Subj)) or verify(trim(PartNumber),'0123456789LS'); */
run;
proc print data=Errors noobs;
run;
o/p:
Subj

PartNumber

Name

0a2

L887X

Fred Beans

004

abcde

Mary Bumpers

Subj

PartNumber

Name

X89

8888S

Gill Sandford

Program 11:
data Errors;
set '/home/jrajeev940/sasuser.v94/errors.sas7bdat';
where anydigit(Name);
run;
proc print data=Errors noobs;
var Subj Name;
run;
o/p:
Subj

Name

003

Alfred 2 Nice

Program 12:
data Errors;
set '/home/jrajeev940/sasuser.v94/errors.sas7bdat';
run;
proc print data=Errors noobs;
where findc(PartNumber,'XD','i');
var Subj PartNumber;
run;
o/p:
Subj

PartNumber

0a2

L887X

004

abcde

Program 13:
data exact within25;
set '/home/jrajeev940/sasuser.v94/social.sas7bdat';
if SS1 eq SS2 then output exact;
else if spedis(SS1,SS2) le 25 then output within25;
run;
title "Listing of EXACT";
proc print data=exact noobs;
run;
title "Listing of WITHIN25";
proc print data=within25 noobs;
run;

o/p:

Listing of EXACT
SS1

SS2

123-45-6789

123-45-6789

007-77-6767

007-77-6767

Listing of WITHIN25
SS1

SS2

001-34-9876

001-43-9876

102-43-9182

102-43-9188

Program 14:
data Medical;
set '/home/jrajeev940/sasuser.v94/medical.sas7bdat';
run;
proc print data=Medical noobs;
where findw(Comment,'antibiotics');
var Patno Comment;
run;
o/p:
Patno

Comment

002

Patient has been on antibiotics for 10 days

Program 15:
libname Mylib '/home/jrajeev940/sasuser.v94';
data numbers;
set Mylib.names_and_more(keep= Phone);
length Areacode $3;
Areacode = substr(Phone,2,3);
run;
proc print data=numbers;
run;
o/p:
Obs Phone
1 (908)782-1234

Areacode
908

Obs Phone

Areacode

2 (315) 848-8484

315

3 (800)123-4567

800

4 (518)355-1766

518

5 (445)232-2233

445

Program 16:
libname Mylib '/home/jrajeev940/sasuser.v94';
data namelist;
set Mylib.names_and_more(keep= Name);
length Lastname $15;
Name = compbl(Name);
Lastname = scan(Name,2,' ');
run;
proc sort data=namelist;
by Lastname;
proc print data=namelist;
run;
o/p:
Obs Name

Lastname

1 Roger Cody

Cody

2 Michael DeMarco

DeMarco

3 Thomas Jefferson

Jefferson

4 Marco Polo

Polo

5 Brian Watson

Watson

Program 17:
libname Mylib '/home/jrajeev940/sasuser.v94';
data Personal;
set Mylib.personal (drop= Food1-Food8);
substr(SS,1,7) = '******';
substr(AcctNum,5,1) = '-';
run;
proc print data=Personal;
run;
o/p:

Obs SS

Gender

AcctNum

DOB

1 ****** 6789

0192-

11/15/1949

2 ****** 9388

9981-

01/02/1981

3 ****** 1309

1322-

03/29/1988

4 ****** 4655

9899-

07/04/1981

5 ****** 4455

2938-

08/09/1977

You might also like