Hi y'all I hope you're doing well,
I've got a question, i'm a little bit new to sas arrays, and I have a dataset containing variables and obs like this :
| date | A | B | weekdate |
| 01/02/2018 | 17500 | 25682 | 29/01/2018 |
. | . | . | 30/01/2018 |
| . | . | . | 31/01/2018 |
| . | . | . | 01/02/2018 |
| . | . | . | 02/02/2018 |
| . | . | . | 03/02/2018 |
what I want is to copy the values that are in the first row for the vars Date A and B and copy them for each row that is blank, to do so i've tried this ( but does not work)
data want;
set have;
array model(*) Date A B;
do j=1 to dim(model);
do i=1 to dim(model);
model[i,j] = model[i+1,j];
end;
end;
drop i j;
run;but i do not know what seems to be the problem can you please help me ?
You can use a temporary array to hold the non-missing values found in a row, and apply those non-missing values in future rows that have a missing values. The values are processed within a row using a variable based array.
Example:
data have;
attrib
date format=ddmmyy10. informat=ddmmyy10.
A B Length=8
weekdate format=ddmmyy10. informat=ddmmyy10.
;
input date A B weekdate; datalines;
01/02/2018 17500 25682 29/01/2018
. . . 30/01/2018
. . . 31/01/2018
. . . 01/02/2018
. . . 02/02/2018
. . . 03/02/2018
;
data want;
array holdNum(4) _temporary_; /* temporary array, values implicitly retained across rows and not part of output data set */
array varsNum date a b weekdate; /* variable based array allows iterative processing of like typed variables */
set have;
do _i_ = 1 to dim(varsNum);
if not missing(varsNum(_i_))
then holdNum(_i_) = varsNum(_i_); /* save non-missing value for future application */
else varsNum(_i_) = holdNum(_i_); /* assign prior saved non-missing value to a variable via array reference */
end;
run;
You can use a temporary array to hold the non-missing values found in a row, and apply those non-missing values in future rows that have a missing values. The values are processed within a row using a variable based array.
Example:
data have;
attrib
date format=ddmmyy10. informat=ddmmyy10.
A B Length=8
weekdate format=ddmmyy10. informat=ddmmyy10.
;
input date A B weekdate; datalines;
01/02/2018 17500 25682 29/01/2018
. . . 30/01/2018
. . . 31/01/2018
. . . 01/02/2018
. . . 02/02/2018
. . . 03/02/2018
;
data want;
array holdNum(4) _temporary_; /* temporary array, values implicitly retained across rows and not part of output data set */
array varsNum date a b weekdate; /* variable based array allows iterative processing of like typed variables */
set have;
do _i_ = 1 to dim(varsNum);
if not missing(varsNum(_i_))
then holdNum(_i_) = varsNum(_i_); /* save non-missing value for future application */
else varsNum(_i_) = holdNum(_i_); /* assign prior saved non-missing value to a variable via array reference */
end;
run;
Thank you very very much that works very well
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.