Sample 24663: Shift non-missing values left in each observation of a data set
The sample code on the Full Code tab illustrates how to shift non-missing values left in each observation of a data set. The sample code does the following:
- Creates a test data set with missing values in some variables.
- Reads that data set to output a new data set.
- The new data set has a new set of variables containing the values of the original set of variables with all the non-missing values moved to the left-most variables.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
The sample code below illustrates how to shift non-missing values left in each observation of a data set.
/* Create sample data set */
data test;
infile datalines truncover dlm=' ';
input v1 $ v2 $ v3 $ v4 $ v5 $;
datalines;
test1 test2 test3 . test5
. test2 test3 test4 test5
test1 . test3 . test5
test1 . . test4 .
test1 test2 test3 test4 test5
;
run;
/* Use ARRAY processing to assign non-missing values into new variables */
data shifted (drop=v1-v5 j i);
set test;
array in v1-v5;
array out $ o1-o5;
j=1;
do i=1 to 5;
if in(i) ne ' ' then do;
out(j)=in(i);
j+1;
end;
end;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs o1 o2 o3 o4 o5
1 test1 test2 test3 test5
2 test2 test3 test4 test5
3 test1 test3 test5
4 test1 test4
5 test1 test2 test3 test4 test5
Create a SAS data set where all the non-missing values are shifted left in each observation, and the missing values are moved to the right-most variables in the each observation.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> Array processing
|
Date Modified: | 2005-12-08 11:34:18 |
Date Created: | 2004-09-30 14:09:02 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |