기억이 안나는 경우가 있어서 정리해 올립니다.
* SAS Variable Lists
* 질문 : http://cafe.daum.net/statsas/B3m/13486
1. SQL에서 사용
data back;
input (ID NAME Year_200801 Year_200802 Year_201001 Year_201112) ($);
cards;
a b c d e f
;
* Keep 문장에서 : 옵션을 사용하여서 Year로 시작하는 변수명을 모두 Keep 하는 방법이 있고,
* 좀 조건이 복잡하면 Macro로 년월을 생성하는 방법을 고려해 볼 수 있겠습니다.;
proc sql;
create table back1 as
select *
from back(keep=Year:);
quit;
2. SUM함수에서 이용(데이터 스텝)
두번째 문제는 SQL에서는 바로 생각이 안나고, 일반 데이터 스텝에서 SUM 함수의 of 옵션을 사용하시면 되겠네요
http://www.statwith.pe.kr/SAS/1.FUNCTION/F0389.htm
data back;
input (ID NAME) ($) Year_200801 Year_200802 Year_200803 Year_201112;
cards;
a b 1 2 3 4
;
data back1;
set back;
Year_2008 = sum(of Year_2008:);
run;
* SAS도움말 : http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695105.htm
Definition
A SAS variable list is an abbreviated method of referring to a list of variable names. SAS enables you to use the following variable lists:
- numbered range lists
- name range lists
- name prefix lists
- special SAS name lists.
With the exception of the numbered range list, you refer to the variables in a variable list in the same order that SAS uses to keep track of the variables. SAS keeps track of active variables in the order that the compiler encounters them within a DATA step, whether they are read from existing data sets, an external file, or created in the step. In a numbered range list, you can refer to variables that were created in any order, provided that their names have the same prefix.
You can use variable lists in many SAS statements and data set options, including those that define variables. However, they are especially useful after you define all of the variables in your SAS program because they provide a quick way to reference existing groups of data.
Note: Only the numbered range list is used in the RENAME= option.
--------------------------------------------------------------------------------------------------
Numbered Range Lists
Numbered range lists require you to have a series of variables with the same name, except for the last character or characters, which are consecutive numbers. For example, the following two lists refer to the same variables:
x1,x2,x3,...,xn
x1-xn
In a numbered range list, you can begin with any number and end with any number as long as you do not violate the rules for user-supplied variable names and the numbers are consecutive.
For example, suppose you decide to give some of your numeric variables sequential names, as in VAR1, VAR2, and so on. Then, you can write an INPUT statement as follows:
input idnum name $ var1-var3;
Note that the character variable NAME is not included in the abbreviated list.
--------------------------------------------------------------------------------------------------
Name Range Lists
Name range lists rely on the order of variable definition, as shown in the following table:
Name Range Lists
Variable List | Included Variables |
x--a | all variables in order of variable definition, from X to A inclusive |
x-numeric-a | all numeric variables from X to A inclusive |
x-character-a | all character variables from X to A inclusive |
You can use the VARNUM option in PROC CONTENTS to print the variables in the order of definition.
For example, consider the following INPUT statement:
input idnum name $ weight pulse chins;
In later statements you can use these variable lists:
/* keeps only the numeric variables idnum, weight, and pulse */
keep idnum-numeric-pulse;
/* keeps the consecutive variables name, weight, and pulse */
keep name--pulse;
--------------------------------------------------------------------------------------------------
Name Prefix Lists
Some SAS functions and statements enable you to use a name prefix list to refer to all variables that begin with a specified character string:
sum(of SALES:)
tells SAS to calculate the sum of all the variables that begin with "SALES," such as SALES_JAN, SALES_FEB, and SALES_MAR.
--------------------------------------------------------------------------------------------------
Special SAS Name Lists
Special SAS name lists include
_NUMERIC_
specifies all numeric variables that are already defined in the current DATA step.
_CHARACTER_
specifies all character variables that are currently defined in the current DATA step.
_ALL_
specifies all variables that are currently defined in the current DATA step.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.