SAS Tip : Specify a list of variables

Deepanshu Bhalla 13 Comments
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;
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 13 Responses to "SAS Tip : Specify a list of variables"
  1. Brief but excellent! Just love the way it progressed-- Thanks.

    ReplyDelete
  2. Perfect! Many concepts explained in simple langauge

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

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

    ReplyDelete
  5. Hi, Drop set option is been used above but still it sums up the numbers, how can it be....
    is it that, the drop data set option not used along with set statement, if so, then why is it used for no use in this particular data step... HELP..!!!

    ReplyDelete
    Replies
    1. Drop has been used in data step and not set step..so sum variables are created and then while data goes for output, then q1--q5 is dropped

      Delete
  6. thank you so much sir!!!this site is very very useful freshers like me. keep providing such an amazing pages.

    ReplyDelete
Next → ← Prev