BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
skavli
Calcite | Level 5

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 :

 

dateABweekdate
01/02/2018175002568229/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 ?

 

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

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;

View solution in original post

2 REPLIES 2
RichardDeVen
Barite | Level 11

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;
skavli
Calcite | Level 5

Thank you very very much that works very well

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 730 views
  • 0 likes
  • 2 in conversation