SAS : WildCard Character

Deepanshu Bhalla 7 Comments

In this tutorial, you will learn how to use wildcard character in SAS.

Example 1 : Keeping Variables Starting with a Certain Character

In this case, the COLON (:) within KEEP= option tells SAS to select all the variables starting with a specific character i.e. 'X'.

DATA READIN;
INPUT ID X1 X_T $;
CARDS;
2 3 01
3 4 010
4 5 022
5 6 021
6 7 032
;
RUN;  
DATA READIN2;
SET READIN (KEEP = X:);
RUN;  
Example 2 : Filter data using wildcard character

In this case, we are selecting all the observations (rows) starting with the character '01'.

DATA READIN2;
SET READIN;
IF X_T =: '01';
RUN;
Example 3 : Use of WildCard in IN Operator

Here we are selecting all the observations starting with the character '01' or '02'.

DATA READIN2;
SET READIN;
IF X_T IN: ('01', '02');
RUN;
Example 4 : Use of WildCard in GT LT (> <) Operators
DATA READIN2;
SET READIN;
IF X_T >: '01';
RUN;

Here we are selecting all the cases from character '01' up alphabetically.

Example 5 : WildCard in Function
data example3;
set temp2;
total =sum(of height:);
run;

The TOTAL = SUM(OF height:); statement calculates the sum of all variables that start with height and assigns the result to the total variable.

Example 6 : WildCard in Array

The following SAS code double transposes the dataset to generate separate variables of height for both males and females and then selects them in an array using a wildcard.

proc sort data = sashelp.class out=class;
by name sex;
run;

proc transpose data = sashelp.class out=temp;
by name sex;
var height;
run;

proc transpose data = temp DELIMITER=_ out=temp2(drop=_name_);
by name;
var col1;
id _name_ sex;
run;

proc sql noprint;
select CATS('new_',name) into: newnames separated by " "
from dictionary.columns
where libname = "WORK" and memname = "TEMP2" and name like "Height_%";
quit;

data myoutput;
set temp2;
array h(*) height:;
array newh(*) &newnames.;
do i = 1 to dim(h);
newh{i} = h{i}*2;
end;
drop i;
run;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 7 Responses to "SAS : WildCard Character"
  1. Thank You Deepanshu, This Is great! Very Efficient!

    ReplyDelete
  2. In the example 6 in the last SQL step in the WHERE clause must be upcase(name) like "height_%"; (or use "Height_%").

    ReplyDelete
  3. Hello ,

    Can you please explain Example 6 : WildCard in Array concept in detail?

    ReplyDelete
  4. i didn't get the Array part. what it does and what would be the output

    ReplyDelete
  5. Hello,
    I always really appreciate your contribution.
    your contents are really easy and useful.
    Please don't stop posting.
    Thank and Have a great day!

    ReplyDelete
  6. This is very useful tutorial.Thank you

    ReplyDelete
Next → ← Prev