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
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);The output is shown in the image below -
set dummy;
sum = sum(of q1-q4);
sum1 = sum(of q1--q4);
run;
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);How to use double dash in array
set dummy;
sum = sum(of _numeric_);
run;
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.
More Dash Symbol Usage
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
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;
wowwwwwwwwwwww
ReplyDeleteBrief but excellent! Just love the way it progressed-- Thanks.
ReplyDeletePerfect! Many concepts explained in simple langauge
ReplyDeleteAwesome work bro! Thanks a ton!!
ReplyDeleteclear and concise, appreciate your work and thankful to you - Gautam Singh
ReplyDeleteExcellent...
ReplyDeletereally very usefull lession.
ReplyDeleteHi, Drop set option is been used above but still it sums up the numbers, how can it be....
ReplyDeleteis 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..!!!
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
DeleteNice. Very usefull
ReplyDeleteNice. Very usefull
ReplyDeletethank you so much sir!!!this site is very very useful freshers like me. keep providing such an amazing pages.
ReplyDeletethank you so much..
ReplyDelete