SAS Tip : Specify a list of variables

Live Online Training : SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English
- Live Projects
- Scenario Based Questions
- Job Placement Assistance
- Get 10% off till Sept 25, 2017
- Batch starts from October 8, 2017

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

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

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

Related Posts:

15 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
  7. This comment has been removed by the author.

    ReplyDelete
  8. 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
  9. one more add point we can consider is that if we do sum of var1=sum(ofq1-q3) and than sum(of _numeric_) than sum of numeric will consider var1 also as numeric and it's final value is var1 + sum( of q1-q3)

    ReplyDelete

Next → ← Prev