SAS_数组arrays技巧
- 格式:pdf
- 大小:52.66 KB
- 文档页数:8
SAS数组array英⽂参考⼿册Paper 242-30Arrays Made Easy: An Introduction to Arrays and Array Processing Steve First and Teresa Schudrowitz, Systems Seminar Consultants, Inc., Madison, WI ABSTRACTMany programmers often find the thought of using arrays in their programs to be a daunting task, and as a result they often shy away from arrays in their code in favor of better-understood, but more complex solutions. A SAS array is a convenient way of temporarily identifying a group of variables for processing within a data step. Once the array has been defined the programmer is now able to perform the same tasks for a series of related variables, the array elements. Once the basics of array processing are understood arrays are a simple solution to many program scenarios.Throughout this paper we will review the following array topics:1) Why do we need arrays?2) Basic array conceptsa) Definitionb) Elementsc) Syntaxd) Rules3) Using array indexes4) One dimension arrays5) Multi-dimension arrays6) Temporary arrays7) Explicit vs. implicit subscripting8) Sorting arrays9) When to use arrays10) Common errors and misunderstandingsINTRODUCTIONMost mathematical and computer languages have some notation for repeating or other related values. These repeated structures are often called a matrix, a vector, a dimension, a table, or in the SAS data step, this structure is called an array. While every memory address in a computer is an array of sorts, the SAS definition is a group of related variables that are already defined in a data step. Some differences between SAS arrays and those of other languages are that SAS array elements don’t need to be contiguous, the same length, or even related at all. All elements must be character or numeric. WHY DO WE NEED ARRAYS?The use of arrays may allow us to simplify our processing. We can use arrays to help read and analyze repetitive data with a minimum of coding.An array and a loop can make the program smaller. For example, suppose we have a file where each record contains 24 values with the temperatures for each hour of the day. These temperatures are in Fahrenheit and we need to convert them to 24 Celsius values. Without arrays we need to repeat the same calculation for all 24 temperature variables:data;input etc.celsius_temp1 = 5/9(temp1 – 32);celsius_temp2 = 5/9(temp2 – 32);. . .celsius_temp24 = 5/9(temp24 – 32);run;An alternative is to define arrays and use a loop to process the calculation for all variables:data;input etc.array temperature_array {24} temp1-temp24;array celsius_array {24} celsius_temp1-celsius_temp24;do i = 1 to 24;celsius_array{i} = 5/9(temperature_array{i} – 32);end;run;While in this example there are only 24 elements in each array, it would work just as well with hundreds of elements. In addition to simplifying the calculations, by defining arrays for the temperature values we could also have used them in the input statement to simplify the input process. It should also be noted, while TEMP1 is equivalent to the first element, TEMP2 to the second etc., the variables do not need to be named consecutively. The array would work just as well with non-consecutive variable names.array sample_array {5} x a i r d;In this example, the variable x is equivalent to the first element, a to the second etc.Arrays may also be used to provide table lookups. For instance, we have different percentage rates that will be applied to a representative’s sales amounts to determine their commission. The percentage amount may be stored within an array structure, and then the array could provide a location to look up the appropriate percentage for inclusion in the commission calculation.BASIC ARRAY CONCEPTSArrays within SAS are different than arrays in other languages. SAS arrays are another way to temporarily group and refer to SAS variables. A SAS array is not a new data structure, the array name is not a variable, and arrays do not define additional variables. Rather, a SAS array provides a different name to reference a group of variables.The ARRAY statement defines variables to be processed as a group. The variables referenced by the array are called elements. Once an array is defined, the array name and an index reference the elements of the array. Since similar processing is generally completed on the array elements, references to the array are usually found within DO groups.ARRAY STATEMENTThe statement used to define an array is the ARRAY statement.array array-name {n} <$> array-elements <(initial-values)>;The ARRAY statement is a compiler statement within the data step. In addition, the array elements cannot be used in compiler statements such as DROP or KEEP. An array must be defined within the data step prior to being referenced and if an array is referenced without first being defined, an error will occur. Defining an array within one data step and referencing the array within another data step will also cause errors, because arrays exist only for the duration of the data step in which they are defined. Therefore, it is necessary to define an array within every data step where the array will be referencedThe ARRAY statements provides the following information about the SAS array:array-name – Any valid SAS namen – Number of elements within the array$ - Indicates the elements within the array are character type variableslength – A common length for the array elementselements – List of SAS variables to be part of the arrayinitial values – Provides the initial values for each of the array elementsThe array name will be used as part of the array reference when the array elements are used in processing. The name must follow the same rules as variable names, therefore, any valid SAS name is a valid array name. When naming an array it is best to avoid using an array name that is the same as a function name to avoid confusion. While parentheses or square brackets can be used when referencing array elements, the braces {} are used most often since they are not used in other SAS statements. SAS does place one restriction on the name of the array. The array name may not be the same name as any variable on the SAS data set.The elements for an array must be all numeric or all character. When the elements are character it is necessary to indicate this at the time the array is defined by including the dollar sign ($) on the array statement after the referenceto the number of elements. If the dollar sign is not included on the array statement, the array is assumed to be numeric. When all numeric or all character variables in the data set are to be elements within the array, there are several special variables that may used instead of listing the individual variables as elements. The special variables are:_NUMERIC_ - when all the numeric variables will be used as elements _CHARACTER_ - when all the character variables will be used as elements _ALL_ - when all variables on the data set will be used as elements and the variables are allthe same typeN is the array subscript in the array definition and it refers to the number of elements within the array. A numeric constant, a variable whose value is a number, a numeric SAS expression, or an asterisk (*) may be used as thesubscript. The subscript must be enclosed within braces {}, square brackets [], or parentheses (). In our temperature array temperature_array {24} temp1 – temp24;When the asterisk is used, it is not necessary to know how many elements are contained within the array. SAS will the elements.array allnums {*} _numeric_;When it is necessary to know how many elements are in the array, the DIM function can be used to return the count of elements.do i = 1 to dim(allnums);allnums{i} = round(allnums{i},.1);end;In this example, when the array ALLNUMS is defined, SAS will count the number of numeric variables used aselements of the array. Then, in the DO group processing, the DIM function will return the count value as the ending range for the loop.ARRAY REFERENCESWhen an array is defined with the ARRAY statement SAS creates an array reference. The array reference is in the following form:array-name{n}The value of n will be the element’s position within the array. For example, in the temperature array defined abovethe temperature for 1:00 PM is in the variable TEMP13. The array element has been assigned the 13thposition within the array. Therefore, the array reference will be:temperature_array{13}The variable name and the array reference are interchangeable. When an array has been defined in a data step either the variable name or the array reference may be used.Variable Name Array Referencetemp1temperature_array{1}temp2temperature_array{2}temp3temperature_array{3}temp4temperature_array{4}temp5 temperature_array{5}An array reference may be used within the data step in almost any place other SAS variables may be used including as an argument to many SAS functions. If the data step does not have an ARRAY statement to define the array and create the array reference, errors will occur. When an array is referenced within a data step, it must be defined with an ARRAY statement in the same data step.USING ARRAY INDEXESThe array index is the range of array elements. In our temperature example, we are looking at temperatures for each of the 24 hours of the day. The array is defined as:array temperature_array {24} temp1 – temp24;Another variation in SAS arrays from arrays within other languages is, subscripts are 1-based by default where arrays in other languages may be 0-based. When we set the array bounds with the subscript and only specify the number of elements within the array as our upper bound, the lower bound is the default value of 1. In our example, the index begins with the lower bound of 1 and ends with the upper bound of 24.There may be scenarios when we want the index to begin at a lower bound other than 1. This is possible by modifying the subscript value when the array is defined. For this example we are using our same temperature variables. Only this time we only want the temperatures for the daytime, temperatures 6 through 18. In this example the array is defined as:array temperature_array {6:18} temp6 – temp18;The subscript will be written as the lower bound and upper bound of the range, separated by a colon.This technique can simplify array usage by using natural values for the index. Examples of this might be to use a person’s age, or to use a year value to get to the correct element.ONE DIMENSION ARRAYSA simple array may be created when the variables grouped together conceptually appear as a single row. This is known as a one-dimensional array. Within the Program Data Vector the variable structure may be visualized as: temperature_array{1}{2} {3}{4}{5} (24)Temperature Variables temp1Temp2temp3temp4temp5…temp24The array statement to define this one-dimensional array will be:array temperature_array {24} temp1 – temp24;The array has 24 elements for the variables TEMP1 through TEMP24. When the array elements are used within the data step the array name and the element number will reference them. The reference to the ninth element in the temperature array is:temperature_array{9}MULTI-DIMENSION ARRAYSA more complex array may be created in the form of a multi-dimensional array. Multi-dimensional arrays may be created in two or more dimensions. Conceptually, a two-dimensional array appears as a table with rows and columns. Within the Program Data Vector the variable structure may be visualized as:2nd Dimension SALE_ARRAY{r,1}{r,2}{r,3}{r,4}…{r,12} SalesVariables{1,c}SALES1SALES2SALES3SALES4…SALES12Expense Variables {2,c}EXP1EXP2EXP3EXP4…EXP121st DimensionCommissionVariables{3,c}COMM1COMM2COMM3COMM4…COMM12 The array statement to define this two-dimensional array will be:array sale_array {3, 12} sales1-sales12 exp1-exp12 comm1-comm12;The array contains three sets of twelve elements. When the array is defined the number of elements indicates the number of rows (first dimension), and the number of columns (second dimension). The first dimension of the array is the three sets of variable groups: sales, expense, and commission. The second dimension is the 12 values within thegroup. When the array elements of a multi-dimensional array are used within the data step the array name and the element number for both dimensions will reference them. The reference to the sixth element for the expense group in the sales array is:sale_array{2,6}Three and more dimensions can be defined as well. It should be noted that if a PROC PRINT is run, only the actual variable names are displayed instead of the array elements, so it can sometimes be difficult to visualize the logical structure. TEMPORARY ARRAYSA temporary array is an array that only exists for the duration of the data step where it is defined. A temporary array is useful for storing constant values, which are used in calculations. In a temporary array there are no corresponding variables to identify the array elements. The elements are defined by the key word _TEMPORARY_.When the key word _TEMPORARY_ is used, a list of temporary data elements is created is created in the Program Data Vector. These elements exist only in the Program Data Vector and are similar to pseudo-variables.array{1}{2}{3}{4}{5}{6}VariablesValues0.050.080.120.200.270.35One method of setting the values for a temporary array’s elements is to indicate initial values in the ARRAY statement. Therefore, a temporary array of rate values might be defined as follows:array rate {6} _temporary_ (0.05 0.08 0.12 0.20 0.27 0.35);The values for the temporary data elements are automatically retained across iterations of the data step, but do not appear in the output data sets. With a few exceptions, temporary data elements behave in a manner similar to variables. Temporary data elements do not have names, therefore they may only be referenced using the array reference. The asterisk subscript cannot be used when defining a temporary array and explicit array bounds must be specified for temporary arrays.We are now able to apply the constant values defined in the array. For example: when a customer is delinquent in payment of their account balance, a penalty is applied. The amount of the penalty depends upon the number of months that the account is delinquent. Without array processing this IF-THEN processing would be required to complete the calculation:if month_delinquent eq 1 then balance = balance + (balance * 0.05);else if month_delinquent eq 2 then balance = balance + (balance * 0.08);else if month_delinquent eq 3 then balance = balance + (balance * 0.12);else if month_delinquent eq 4 then balance = balance + (balance * 0.20);else if month_delinquent eq 5 then balance = balance + (balance * 0.27);else if month_delinquent eq 6 then balance = balance + (balance * 0.35);By placing the penalty amounts into a temporary array, the code for calculating the new balance can be simplified. The penalty amounts have been stored in the temporary array RATE. The new account balance with the penalty can now be calculated as:array rate {6} _temporary_ (0.05 0.08 0.12 0.20 0.27 0.35);if month_delinquent ge 1 and month_delinquent le 6 thenbalance = balance + (balance * rate{month_delinquent});In addition to simplifying the code, the use of the temporary array also improves performance time.Setting initial values is not required on the ARRAY statement. The values within a temporary array may be set in another manner within the data step.array rateb {6} _temporary_;do i = 1 to 6;rateb{i} = i * 0.5;end;Earlier versions of SAS originally defined arrays in a more implicit manner as follows:array array-name<(index-variable)> <$> array-elements <(initial-values)>; In an implicit array, an index variable may be indicated after the array name. This differs from the explicit array previously discussed where a constant value or an asterisk, as the subscript, denotes the array bounds. When an implicit array is defined, processing for every element in the array may be completed with a DO-OVER statement. The variable specified as the index variable is tied to the array and should only be used as the array index.Example:array item(j) $ 12 x1-x12;do over item;put item;end;When referencing the array, only the array name is specified. Thus, the implied notation, instead of the more explicit mode described earlier. If a program needed to reference by index-variable, that can also be done, but must be specified in a separate statement.array item(j) $ 12 x1-x12;do j=1 to 12;put item;end;Because of the difficulty understanding the more cryptic implicit arrays, explicit arrays are recommended. Implicit array support was left in SAS only to insure older programs would continue to run.SORTING ARRAYSThere are several new experimental call routines in SAS 9.1 that can be used to sort across a series of variables. SORTN can be used to sort numeric variables and SORTQ for character fields. An example of sorting several numeric variables is as follows:data _null_;array xarry{6} x1-x6;set ds1;call sortn(of x1-x6);run;When an observation from ds1 is processed the values brought into the Program Data Vector appear as follows:xarry{1}{2}{3}{4}{5}{6}Variables x1x2x3x4x5x6Values0.270.120.200.080.350.05The SORTN call routine will sort the values of the variables in ascending order and replace the Program Data Vector values with the new sorted values. Thus, after the call routine the Program Data Vector will appear as follows:xarry{1}{2}{3}{4}{5}{6}Variables x1x2x3x4x5x6Values0.050.080.120.200.270.35Because the values for the variables are now different the value selected by the array reference will be affected. For instance, to calculate rate we must add the value in the array reference to 0.75 as follows:rate = 0.75 + xarry{i};If the calculation is completed prior to the SORTN call routine and i is equal to 3, rate would be 0.95. On the other hand, if the same calculation were to be completed after the call routine, rate would be 0.87.It makes sense to use arrays when there are repetitive values that are related and the programmer needs to iterate though most of them. The combination of arrays and do loops in the data step lend incredible power to programming. The fact that the variables in the array do not need to be related or even contiguous makes them even more convenient to use.COMMON ERRORS AND MISUNDERSTANDINGSCommon errors and misunderstandings occur in array processing. This section will review several of these and how they are resolved.INVALID INDEX RANGEIn the processing of array references, it is easy to use an index value from outside the array bounds. A typical instance when this occurs is while looping through the array references with DO group processing.data dailytemp;set tempdata;array temperature_array {24} temp1-temp24;array celsius_array {24} celsius_temp1-celsius_temp24;do until (i gt 24);i = 1;celsius_array{i} = 5 / 9 * (temperature_array{i} – 31);end;i=0;drop i;run;In the scenario a DO-UNTIL loop is used to process the 24 different temperatures. When a DO-UNTIL loop is processed the evaluation of the expression is not completed until the bottom of the loop. After processing for i equal to 24, the expression is still true. Therefore, processing remains within the loop and i will be incremented to 25. Both arrays used in the calculation were defined with only 24 elements. An index value of 25 is greater than the array’s upper bound. As a result, the data error message “Array subscript out of range” will be received.There are two possible resolutions to this scenario. One possibility is to continue using the DO-UNTIL loop, but change the expression to check for I greater than 23:do until (i gt 23);i = 1;celsius_array{i} = 5 / 9 * (temperature_array{i} – 31);end;Another possibility is to modify the loop to DO-WHILE processing. A DO-WHILE loop evaluates the expression first and if the expression is true the processing within the loop will continue. Therefore, the modified code for using aDO-WHILE loop would be:do while (i le 24);i = 1;celsius_array{i} = 5 / 9 * (temperature_array{i} – 31);end;FUNCTION NAME AS AN ARRAY NAMEThe use of a function name as an array name on the ARRAY statement may cause unpredictable results. If a function name is used as an array name, SAS will no longer recognize the name as a function name. Therefore, rather than seeing parenthetical values as function arguments, SAS now sees those values as the index value for an array reference.When a function name is used to define an array name SAS will provide a warning in the log. If the function is then used within the data step error messages may be received as result of SAS attempting to interpret the function arguments as an array reference.8 data dailytemp;9 set tempdata;10 array mean {24} temp1-temp24;WARNING: An array is being defined with the same name as a SAS-supplied or user-defined function. Parenthesized references involving this name will be treated as array references and not function references.11 do i = 1 to 24;12 meantemp = mean(of temp1-temp24);ERROR: Too many array subscripts specified for array MEAN.13 end;14run;15Avoiding function names as array names is a simple resolution in this scenario.ARRAY REFERENCED IN MULTIPLE DATA STEPS, BUT DEFINED IN ONLY ONEEvery data step where an array is referenced it must be defined within the step with an ARRAY statement. A sample program contains the following data steps:data dailytemp;set tempdata;array temperature_array {24} temp1-temp24;array celsius_array {24} celsius_temp1-celsius_temp24;do i = 1 to 24;celsius_array{i} = 5 / 9 * (temperature_array{i} – 31);end;run;data celsius;set dailytemp;do i = 1 to 24;if celsius_array{i} lt 0 then tempdesc = ‘below freezing’;else if celsius_array{i} gt o then tempdesc = ‘above freezing’;end;run;The first data step contains the definition of the array CELSIUS_ARRAY on the second ARRAY statement. References to the array do not cause any issues. The second data step has two references to the array but the array has not been defined within this data step.The resolution to this issue requires the inclusion of the ARRAY statement to define the array within every data step where it is referenced. Therefore, the second data step would be modified as follows:data celsius;set dailytemp;array celsius_array {24} celsius_temp1-celsius_temp24;do i = 1 to 24;if celsius_array{i} lt 0 then tempdesc = ‘below freezing’;else if celsius_array{i} gt o then tempdesc = ‘above freezing’;end;run;CONCLUSIONArrays are not a mystery, but a wonderful tool for Data step programmers that need to reference repetitive values. CONTACT INFORMATIONYour comments and questions are valued and encouraged. Contact the author at:Steve FirstSystems Seminar Consultants, Inc.2997 Yarmouth Greenway DrMadison, WI 53711Work Phone: (608) 279-9964 x302Fax: (608) 278-0065Email: sfirst@/doc/446319a7b0717fd5360cdca6.htmlWeb: /doc/446319a7b0717fd5360cdca6.htmlTeresa SchudrowitzSystems Seminar Consultants, Inc.2997 Yarmouth Greenway DrMadison, WI 53711Work Phone: (608) 279-9964 x309Fax: (608) 278-0065Email: tschudrowitz@/doc/446319a7b0717fd5360cdca6.htmlWeb: /doc/446319a7b0717fd5360cdca6.htmlSAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ? indicates USA registration.Other brand and product names are trademarks of their respective companies.。
sas中用于对数据集行列互换的方式SAS中用于对数据集行列互换的方式在SAS中,我们经常需要对数据集进行行列互换,以便更好地进行数据分析和处理。
SAS提供了多种方法来实现这一目的,本文将介绍其中两种常用的方法。
方法一:使用PROC TRANSPOSEPROC TRANSPOSE是SAS中用于对数据集进行行列互换的过程。
它可以将数据集中的行转换为列,或将列转换为行。
PROC TRANSPOSE的语法如下:PROC TRANSPOSE DATA=dataset OUT=transposed_dataset; BY variable;ID variable;VAR variable;RUN;其中,DATA参数指定要转换的数据集,OUT参数指定转换后的数据集名称。
BY参数指定按照哪个变量进行分组,ID参数指定要转换的列变量,VAR参数指定要转换的值变量。
例如,我们有一个数据集sales,其中包含了不同地区的销售额数据:data sales;input region $ sales;datalines;North 1000South 2000East 1500West 1800;我们可以使用PROC TRANSPOSE将其行列互换,代码如下:proc transpose data=sales out=transposed_sales;var sales;run;运行后,我们得到了一个新的数据集transposed_sales,其中包含了转换后的数据:变量名 | North | South | East | West-------|-------|-------|------|------sales | 1000 | 2000 | 1500 | 1800方法二:使用DATA步骤除了PROC TRANSPOSE,我们还可以使用DATA步骤来实现数据集的行列互换。
具体方法是将原始数据集中的每一列作为新数据集中的一行,代码如下:data transposed_sales;set sales;array sales_array(*) sales;do i=1 to dim(sales_array);region=scan(vname(sales_array(i)),1,'_');sales=sales_array(i);output;end;drop i sales:;run;在这个代码中,我们首先使用SET语句将原始数据集sales读入,然后使用ARRAY语句将sales变量存储到一个数组sales_array中。
arrays类的四个基本方法嘿,朋友们!今天咱来聊聊那个神奇的arrays 类的四个基本方法呀!你想想看,这就好比是一个武林高手的四招绝技!这第一招呢,就是“sort 方法”,它就像是一个神奇的整理大师,能把一堆杂乱无章的数据瞬间排得整整齐齐。
哎呀,这可太重要啦!就好比你那乱七八糟的房间,经过它这么一整理,立马变得井井有条,找东西都方便多了呢!接着就是“fill 方法”啦,它就像是一个超级填充侠!可以把数组填满特定的值。
这多厉害呀,就像给一个空荡荡的瓶子灌满了神奇的药水,一下子就让数组变得充实起来。
还有“copyOf 方法”,这简直就是个复制小精灵呀!能把原来的数组复制一份出来,而且还可以按照你的要求调整大小呢。
这就好像你有个特别喜欢的玩具,然后你又变出了一个一模一样的,是不是很有趣呀?最后一个“toString 方法”,那可是个厉害的展示高手!它能把数组里的内容以一种清晰明了的方式呈现出来。
就像把隐藏在幕后的演员们都拉到舞台上,让大家能清楚地看到他们的精彩表演。
你说这四个基本方法是不是超级实用呀?它们在编程的世界里可发挥着巨大的作用呢!没有它们,很多复杂的任务可就变得难上加难啦!所以呀,我们可得好好掌握它们,就像武林高手熟练运用自己的绝技一样。
当你在编程中遇到问题的时候,想想这四个方法呀,说不定它们就能帮你轻松解决难题呢!它们就像是你手中的秘密武器,随时准备为你披荆斩棘。
而且哦,学会使用它们还能让你的代码变得更加简洁高效。
你看,原本需要很多繁琐步骤才能完成的事情,有了它们,可能只需要几行代码就搞定啦!这多厉害呀!总之呢,arrays 类的这四个基本方法可真是太重要啦!它们是编程世界里不可或缺的一部分,就像阳光、空气和水对于我们生活的重要性一样。
所以呀,大家一定要认真学习、好好掌握哦!别小瞧了它们,它们能给你带来意想不到的惊喜和收获呢!相信我,你会爱上这四个神奇的基本方法的!。
文章标题:如何使用SAS提取字符串中的数字并以符号分割随着数据分析和处理的需求不断增加,SAS作为一种常用的统计分析软件,被广泛应用于数据挖掘、商业分析和决策支持等领域。
在实际的数据处理过程中,经常会遇到需要从字符串中提取数字并以符号进行分割的需求,特别是在处理包含特定格式的数据时,这一需求显得尤为重要。
本文将向您介绍如何使用SAS来实现这一功能,以及我的个人观点和理解。
在SAS中,我们可以使用正则表达式和一些内置的函数来实现从字符串中提取数字并以符号分割的功能。
下面,我将分几个步骤来说明具体的操作方法。
步骤一:使用PRXPARSE函数创建正则表达式模式在SAS中,我们可以使用PRXPARSE函数来创建一个正则表达式的模式,用于匹配字符串中的数字。
如果我们需要从字符串中提取出所有的数字,可以使用类似“\d+”这样的正则表达式。
其中,\d表示匹配数字字符,+表示匹配一个或多个数字字符。
步骤二:使用PRXMATCH函数进行匹配一旦创建了正则表达式模式,我们就可以使用PRXMATCH函数来在字符串中进行匹配。
PRXMATCH函数返回的是第一个匹配的位置,我们可以利用这个位置来进一步处理。
步骤三:使用SUBSTR函数进行分割当我们找到了数字在字符串中的位置后,可以使用SUBSTR函数来对字符串进行分割。
通过指定起始位置和长度,可以把字符串分割成多个部分,进而获得我们需要的数字部分。
步骤四:总结和回顾通过以上的操作步骤,我们成功地实现了使用SAS提取字符串中的数字并以符号分割的功能。
在实际应用中,我们还可以根据具体的需求进行进一步的处理,比如将提取出的数字进行求和、平均值计算、或者进行其他的统计分析。
个人观点和理解对于SAS提取字符串中的数字并以符号分割这一功能,我认为它在实际工作中具有非常重要的意义。
在实际的数据分析或报表生成中,经常会遇到需要对特定格式的字符串进行处理的情况,而这些字符串中往往包含着一些重要的数字信息。
arrays的aslist方法Arrays类是Java中提供的一个工具类,它包含了各种用于操作数组的方法。
其中,asList()方法是Arrays类中的一个静态方法,它可以将一个数组转换为一个List集合。
该方法的作用是将数组中的元素作为List集合的元素,从而方便地进行集合操作。
使用asList()方法非常简单,只需要将要转换的数组作为参数传入即可。
例如,我们有一个整型数组arr,想将它转换为一个List集合,可以通过以下代码实现:```int[] arr = {1, 2, 3, 4, 5};List<Integer> list = Arrays.asList(arr);```上述代码中,我们首先定义了一个整型数组arr,然后使用asList()方法将该数组转换为一个List集合。
通过这样的转换,我们就可以使用List集合提供的各种方法来操作数组中的元素了。
使用asList()方法转换数组为List集合的好处是,可以直接使用List 集合提供的方法来操作数组元素,而不需要手动编写循环遍历数组的代码。
例如,我们可以使用List集合的get()方法获取指定位置的元素,使用size()方法获取集合的大小,使用contains()方法判断集合中是否包含某个元素等等。
然而需要注意的是,通过asList()方法转换得到的List集合是一个固定长度的集合,不能进行增删操作。
这是因为asList()方法返回的是一个由原数组支持的固定大小的List集合,对该集合进行增删操作会导致UnsupportedOperationException异常的抛出。
如果需要操作数组元素的增删操作,可以将asList()方法返回的List集合转换为ArrayList集合,如下所示:```int[] arr = {1, 2, 3, 4, 5};List<Integer> list = new ArrayList<>(Arrays.asList(arr));```上述代码中,我们使用Arrays.asList()方法将数组arr转换为一个List集合,然后将该集合转换为ArrayList集合。
numpy asarray函数numpy.asarray函数numpy.asarray函数是将输入转换为数组的函数。
它接受一个序列、一个数组或者一个类数组对象,并返回一个ndarray对象。
如果输入是ndarray类型,则直接返回,否则将输入转换为ndarray类型。
语法numpy.asarray(a, dtype=None, order=None)参数说明a:输入序列、数组或类数组对象。
dtype:数据类型。
可选参数,默认为None。
order:{'C', 'F', 'A'},可选参数,默认为None。
C表示按行优先存储,F表示按列优先存储,A表示根据输入顺序决定存储方式。
返回值返回一个ndarray对象。
示例import numpy as npa = [1, 2, 3]b = np.asarray(a)print(b)输出结果:[1 2 3]分层次详解numpy.asarray函数1. numpy.asarray函数概述numpy.asarray函数是将输入转换为数组的函数。
它接受一个序列、一个数组或者一个类数组对象,并返回一个ndarray对象。
如果输入是ndarray类型,则直接返回,否则将输入转换为ndarray类型。
2. numpy.asarray函数语法numpy.asarray(a, dtype=None, order=None)3. numpy.asarray函数参数说明a:输入序列、数组或类数组对象。
dtype:数据类型。
可选参数,默认为None。
order:{'C', 'F', 'A'},可选参数,默认为None。
C表示按行优先存储,F表示按列优先存储,A表示根据输入顺序决定存储方式。
4. numpy.asarray函数返回值返回一个ndarray对象。
5. numpy.asarray函数示例import numpy as npa = [1, 2, 3]b = np.asarray(a)print(b)输出结果:[1 2 3]6. numpy.asarray函数实现原理numpy.asarray函数的实现原理可以分为以下几个步骤:(1)判断输入是否为ndarray类型,如果是,则直接返回;(2)如果输入不是ndarray类型,则调用numpy.array函数将其转换为ndarray类型;(3)如果dtype参数不为None,则调用astype方法将数组元素转换为指定数据类型;(4)如果order参数不为None,则调用transpose或reshape 方法重新排列数组元素。
12. SAS数组使用SAS数组(ARRAY语句),主要是对多个变量做相同操作时,可以通过数组存储这些变量,借用数组下标执行循环结构来实现,从而大大简化和缩短程序代码。
SAS数组是存储一组同类型(数值型或字符型)的变量,这些变量可以是已存在的,也可以是新创建的。
一、基本语法ARRAY 数组名[n] <$> 变量列表;说明:(1)n是数组的长度(即变量个数);也可以用“[*]”不指定数组长度,而是让SAS根据变量列表数目自己判断;也可以指定数组的下标范围,例如,array Year[2005:2010] YR2005 - YR2010;(2)若是字符型变量需要加“$”,也可以指定字符的长度(“$1”表示数组元素是1个字节的字符);(3)若变量列表各变量是“相同字符+连续数字”可以简写(下面两句代码功能相同):array Cat8 - Cat12;array Cat8 Cat9 Cat10 Catll Cat12;示例:array store[4] Macys Penneys Sears Target;定义数组store,含有4个数值型变量:Macys,Penneys,Sears,Target 使用数组变量Sears用“store[3]”即可。
注意:数组本身不储存在数据集中,只在数据步中定义和使用,即不会创建变量“store[1],store[2]……”;例1广播电台KBRK做了一份歌曲的听众调查,对5首歌进行打分,分值在1-5,如果没听过则填9. 数据文件(C:\MyRawData\KBPK.dat)包括了被访者姓名、年龄、以及5首歌的打分:读取数据,将打分为9的改为缺省值。
代码:data songs;infile'c:\MyRawData\KBRK.dat';input City $ 1-15 Age wj kt tr filp ttr;array song[5] wj kt tr filp ttr;do i = 1to5;if song[i] = 9THEN song[i] =.;end;run;proc print data = songs;title'KBRK Song Survey';run;运行结果:注意:循环变量i会自动作为一列新变量写入数据集,要想避免它,需要加上一句“drop i;”。
java中arrays的用法Java中Arrays的用法在Java中,Arrays是一个非常重要的类,它提供了一系列的方法来操作数组。
Arrays类中的方法可以分为以下几类:1. 数组的排序Arrays类中提供了sort方法来对数组进行排序。
sort方法有两种重载形式,一种是对整个数组进行排序,另一种是对数组的一部分进行排序。
sort方法默认使用快速排序算法,但是对于小数组,它会使用插入排序算法来提高效率。
2. 数组的查找Arrays类中提供了binarySearch方法来对已排序的数组进行二分查找。
如果数组中包含多个相同的元素,binarySearch方法无法保证返回哪一个。
如果要查找的元素不存在于数组中,binarySearch方法会返回一个负数,这个负数是要插入这个元素的位置的相反数减一。
3. 数组的复制Arrays类中提供了copyOf方法来复制数组。
copyOf方法有两个参数,第一个参数是要复制的数组,第二个参数是要复制的长度。
如果要复制的长度大于原数组的长度,copyOf方法会用默认值填充新数组的后面部分。
4. 数组的填充Arrays类中提供了fill方法来填充数组。
fill方法有两个参数,第一个参数是要填充的数组,第二个参数是要填充的值。
fill方法可以用来初始化数组,也可以用来清空数组。
5. 数组的比较Arrays类中提供了equals方法来比较两个数组是否相等。
equals方法会比较两个数组的长度和每个元素的值。
如果两个数组的长度不同,equals方法会返回false。
如果两个数组的长度相同,但是有一个元素的值不同,equals方法会返回false。
6. 数组的转换Arrays类中提供了asList方法来将数组转换为List。
asList方法有一个参数,就是要转换的数组。
asList方法返回的List是一个固定长度的List,不能添加或删除元素。
如果要修改List中的元素,可以直接修改数组中的元素。
Arrays and DO Loops in SAS1)ARRAYs•in computer science, an ARRAY is a structure for holding multiple, related items that allows the user to reference the items using matrix notation; like matrices, ARRAYs canbe uni-dimensional or multi-dimensional−the array itself has a name−the elements of the array are referenced through “subscripts”−the number of subscripts corresponds to the number of dimensions•example, suppose that we had survey information regarding a respondent’s monthly participation in the Food Stamp Program over the preceding calendar year−suppose the name of the array was fspart−the array would have 12 elements—a series of binary indicators corresponding to participation in each of the months of the year−the structure for this uni-dimensional ARRAY could be depictedMonth: Jan. Feb. Mar. Apr. May Jun. Jul. Aug.Sep. Oct. Nov.Dec. subscript: 1 2 3 4 5 6 7 8 9 10 11 12 fspart:−so the January element of the array would be fspart{1}; the Feburary element would be fspart{2}, and so on•example, suppose that we had similar survey information regarding monthly Food Stamp Participation but that it covered two years instead of one−the structure for this multi-dimensional ARRAY could be depictedMonth: Jan. Feb. Mar. Apr.May Jun.Jul.Aug.Sep. Oct. Nov.Dec.1 2 3 4 5 6 7 8 9 10 11 12subscriptsYear:t-2 1t-1 2−the array has 24 elements—binary indicators for every month of two years−the element corresponding to participation in May two years ago would befspart{1,5}; the element for May a one year ago would be fspart{2,5}−note that the user usually supplies the interpretation of the subscripts−as should be clear, there are many potential ways to arrange the data2)ARRAYs in SAS•SAS permits the “construction” and use of ARRAYs in DATA steps•the first step in using an ARRAY in SAS is to declare one; this is done through an ARRAY statement11 SAS allows “explicit” and “implicit” definitions of ARRAYs. The implicit definition is an older andless used approach. This course will only cover the explicit definition.−syntax:ARRAY <array-name{subscript(s)}> <$> <length><<array-elements> <(initial values)>;−at a minimum the ARRAY definition must contain the name of the ARRAY and either an indication of the subscripts or a description of the elements themselves−the array-name must be a valid SAS name, similar to a variable name−the subscript or subscripts, which must appear in braces {}, is either a numerical constant, an asterisk, or a range of numbers◊ a numerical constant is the most common specification and would simply give the number of elements in the ARRAY◊if multiple dimensions are desired, the numbers specifying each dimension would be separated by commas◊the asterisk is a wild card character and indicates that the ARRAY has as many elements as are defined subsequently in the ARRAY statement; if the asterisk isused elements must be defined AND the ARRAY can only have one dimension ◊ranges take the form lower:upper where lower is the value of the first subscript in the range and upper is the value of the last; the number of elements would be(upper – lower + 1); ranges are useful when the subscripts are tied to other values such as years−the array elements are usually SAS variables◊the elements must all be of the same type◊they may include existing SAS variables; in which case, the ARRAY serves mainly as an alternative way to reference the variables◊however, they can also be new variables◊if you do not define the elements (or if there are fewer elements than are defined by the subscript), SAS creates new variables that have the array-name as theprefix and the subscript number as the suffix−other ways to define elements◊_NUMERIC_ will define the elements as all of the previously defined numeric variables from the DATA step◊_CHARACTER_ does the same thing with character variables◊_ALL_ will define the elements as all of the previously defined variables from the DATA step (all of the previously defined variables would have to be of the sametype)◊_TEMPORARY_ will define a list of temporary variables; these do not have names, are automatically RETAINED, and are not transferred to the SAS data set that is created by the DATA step−you can fill the initial values of the elements by listing the values in parentheses and separated by blanks or commas at the end of the ARRAY statement−you can also specify whether the elements are character or numeric by using the $ and specify the LENGTH; if these are not specified, the ARRAY statement will use the existing types of the variables−some examples◊for the uni-dimensional Food Stamp Participation ARRAY that we had discussed earlierARRAY fspart{12};would create variables fspart1 – fspart12◊suppose that we already had variables called fsp1 – fsp12; we could also specify the ARRAY asARRAY fspart{12} fsp1 fsp2 fsp3 fsp4 fsp5 fsp6fsp10fsp12;fsp11fsp9fsp8fsp7or even more compactly asfsp1-fsp12;ARRAYfspart{*}◊suppose that we wanted to initialize an ARRAY for fspart1 – fspart12 with zeroes, we would issue the commandARRAY fspart{12} (0 0 0 0 0 0 0 0 0 0 0 0);−now consider the two dimensional example◊suppose that you had a calendar with 24 months of data starting with January from two years ago (fsp1) and continuing to December from last year (fsp24) ◊you could specify a two dimensional ARRAY asARRAY fspart{2,12} fsp1-fsp24;◊note that if you had issued the statementARRAY fspart{2,12};SAS would have constructed variables fspart1 – fspart24•referencing ARRAY elements−after an ARRAY is defined, you can reference an element by typingarray-name{subscript}where array-name is the name that was defined earlier by the ARRAY statement and subscript is a numerical constant or numerical value with the desired subscript index −this specification works just like any other variable specification in SAS and can be used in the same ways that variables are (e.g., in assignment statements, inconditioning statements, etc.)−in assignment statements, ARRAY elements can appear on the left or right side (or both sides)−using our previous uni-dimensional example, the codeARRAY fspart{12} fsp1-fsp12;x = fspart{7};creates a 12-element uni-dimensional ARRAY of Food Stamp Participation indicatorsand assigns the July value to the SAS variable x−this code does the same thingARRAY fspart{12} fsp1-fsp12;i = 7;x = fspart{i};•referencing all of the elements in an ARRAY as a list−if you use the asterisk as the subscript reference, SAS treats the ARRAY as a list of variables−for example, to INPUT the elements of fspart, you could typeARRAY fspart{12} fsp1-fsp12;INPUT fspart{*};−to determine whether someone had participated at all in the Food Stamp Program during the preceding year, you could typeanyfsprt = MAX(of fspart{*});•DIM function−the function DIM(array-name) returns the number of elements in array-name;◊if the ARRAY is multi-dimensional, DIM() returns the number of elements in the first dimension◊to obtain the number of elements in higher dimensions, use DIM n(), where n is the dimension that you are interested in−this function can be useful for writing general code that does not have to be updated if your ARRAY specifications change•out-of-range references−each time that you try to reference an element in an ARRAY, SAS checks the subscripts against the definition for the ARRAY−if the subscript is below 1 or the lower value (if a range is specified) or above the maximum possible subscript value, SAS will issue a run-time error message that an“array subscript is out of range” and stop processing the DATA step3)DO loops•the use of variables as subscript references is a big advantage of ARRAY processing−variable references mean that you can pick out an element of an ARRAY relative to some characteristic of the current observation or relative to some condition that you are processing•an additional tool that extends variable subscript references is the iterative DO loop •the standard syntax for a DO loop isDO <index_variable> = <start_value> TO <stop_value> <BY <increment_value>>;SAS commandsEND;−the DO loop causes the SAS commands to be performed repeatedly−the DO loop first sets the index_variable to the start_value◊the index_variable must be a SAS variable◊the start_value can be either a numerical constant or a numerical variable −the DO loop compares the index_variable to the stop_value (which itself can be a numerical constant or a numerical variable)◊if the index_variable is not yet “past” the stop_value (greater than the value if positive increments are used or less than the value if negative increments areused), SAS performs the statements in the loop◊if the index_variable is “past” the stop_value, SAS “exits” the DO loop by going to the next statement following the END−the DO loop is iterative◊after SAS performs the statements, it returns to the top of the loop (returns to the DO statement)◊the index_variable is then changed by the amount of the increment_value or by 1 if no increment_value is specified; note that the increment_value can be negative ◊the loop continues until the index_variable is past than the stop_value•DO loops are useful in lots of contexts, but they are especially useful for traversing (accessing all the elements of) ARRAYs•example #3.1:−suppose that we have a uni-dimensional ARRAY fspart that describes Food Stamp Participation over the preceding year−suppose also that we want to count the number of months out of the year that someone participated−we could use the codeARRAY fspart{12} fsp1-fsp12;fsmnths = 0;DO month = 1 TO 12;fsmnths = fsmnths + fspart{month};END;•examples 3.2 and 3.3:−consider the same ARRAY, but now assume that we want to measure the number of quarters out of the last year that someone was receiving Food Stamps −we could use code with nested DO loops (example #3.2)ARRAY fspart{12} fsp1-fsp12;fsqtrs = 0;month = 0;DO qtr = 1 TO 4; /* loop over quarters */fsthisqtr = 0;DO qtrmonth = 1 TO 3; /* loop over months in qtr */month = month + 1;fsthisqtr = MAX(fsthistqtr, fspart{month});END; /* end of qtrmonth loop */fsqtrs = fsqtrs + fsthisqtr;END; /* end of qtr loop */ −or code with incremental indexing (example #3.3)ARRAY fspart{12} fsp1-fsp12;fsqtrs = 0;DO month = 3 TO 12 BY 3;fsqtrs = fsqtrs + MAX(fspart{month}, fspart{month-1}, fspart{month-2});END;•example #3.4:−consider a two-dimensional ARRAY, and assume that we want to measure the number of quarters out of the last two years that someone was receiving Food Stamps −we could use code with nested DO loopsARRAY fspart{2,12};fsqtrs = 0;DO year_ndx = 1 TO 2; /* loop over years */DO month = 3 TO 12 BY 3;fsqtrs = fsqtrs + MAX(fspart{month}, fspart{month-1}, fspart{month-2});END; /* end of month loop */END; /* end of year loop */4)Other DO loops•SAS supports two other types of DO loops•DO WHILE loop−syntaxDO WHILE (<logical_expression>);SAS commandsEND;−this loop will execute these commands while the logical expression is true−if the logical expression is false when the loop is entered, SAS skips the commands (effectively the condition is checked at the top of the loop)−for example, we could redo example #3.1 from above with the following code ARRAY fspart{12} fsp1-fsp12;fsmnths = 0;month = 1;DO WHILE (month LE 12);fsmnths = fsmnths + fspart{month};month = month + 1;END;• a similar construct is the DO UNTIL loop−syntaxDO UNTIL (<logical_expression>);SAS commandsEND;−this loop will execute these commands until the logical expression is true−if the logical expression is false when the loop is entered, SAS executes the commands (effectively the condition is checked at the bottom of the loop); this means that DO UNTIL loops are always executed at least once−for example, we could redo the previous example with the following code ARRAY fspart{12} fsp1-fsp12;fsmnths = 0;month = 1;DO UNTIL (month GT 12);fsmnths = fsmnths + fspart{month};month = month + 1;END;•WHILE and UNTIL conditions can also be added to incremental DO loops•the syntax isDO <index_variable> = <start_value> TO <stop_value> <BY <increment_value>><WHILE (<logical_expression>)> <UNTIL(<logical_expression>)>;SAS commandsEND;−the DO loop would check◊the iterative condition◊the WHILE condition, if it is included, and◊the UNTIL condition, if it is included•WARNING: loops can be both tricky and dangerous to program−you need to consider the conditions very carefully; as the foregoing discussion indicates, the distinctions between different types of loops can be very subtle −as importantly, you need to verify that the conditions that terminate a loop will be met at some point−if the conditions are not met, you could create an “endless loop”−consider the following codeARRAY fspart{12} fsp1-fsp12;fsmnths = 0;month = 1;DO WHILE (month LE 12);fsmnths = fsmnths + fspart{month};END;−we’ve made a mistake; the variable month is never incremented−because of this the WHILE condition always remains true−SAS will continue to process this statement until you either◊issue a “break” (the red exclamation point in the black circle in the SAS tool bar across the top of the SAS window) or◊shut down SAS。