SAS Tip : Specify a list of variables

Suppose you have a list of variables. You don't want to type the name of each variable to define them in a function or array. You are looking for a shortcut to accomplish this task.

Create a dataset with a list of variables

Q1 Q3 Q4 Q2 Q6 BU Q5
1 2 3 5 sa an 3
2 4 3 6 sm sa 4
6 5 3 8 cb na 3
data dummy;
input q1 q3 q4 q2 q6$ bu$ q5;
cards;
1 2 3 5 sa an 3
2 4 3 6 sm sa 4
6 5 3 8 cb na 3
;
run;
How to specify a list of variables

A single dash (-) is used to specify consecutively numbered variables. For example : q1-q4;

A double dash (--) is used to specify variables based on the order of the variables as they appear in the file, regardless of the name of the variables.
data dummy1 (drop= q1--q5);
set dummy;
sum = sum(of q1-q4);
sum1 = sum(of q1--q4);
run;
The output is shown in the image below -


In the above program, q1-q4 includes q1,q2,q3 and q4, whereas q1--q4 includes q1,q3 and q4 only as they appear the same way in file.
How to specify all NUMERIC variables
data dummy1 (drop= q1--q5);
set dummy;
sum = sum(of _numeric_);
run;
How to use double dash in array

The following program subtracts one from values in variables q1,q3 and q4.
data dummy1;
set dummy;
array vars q1--q4;
do over vars;
vars = vars - 1;
end;
run;
How to use numeric variables in array

The following program subtracts one from values in numeric variables.
data dummy1;
set dummy;
array vars _numeric_;
do over vars;
vars = vars - 1;
end;
run;

More Dash Symbol Usage

1. Print all NUMERIC variables from q1 through q6.
proc print;
var q1-numeric-q6;
run;


2.  Print all CHARACTER variables from q1 through q6.
proc print;
var q1-character-q6;
run;
3.  Print all CHARACTER variables.
proc print;
var _character_;
run;

SAS Tutorials : 100 Free SAS Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

9 Responses to "SAS Tip : Specify a list of variables"

  1. Brief but excellent! Just love the way it progressed-- Thanks.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Perfect! Many concepts explained in simple langauge

    ReplyDelete
  4. Awesome work bro! Thanks a ton!!

    ReplyDelete
  5. clear and concise, appreciate your work and thankful to you - Gautam Singh

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete

Next → ← Prev