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

Obs  Subj  Dx1  Dx2  Dx3

1       001   450  430   410
2       002   250  240   .
3       003   410  250   500
4       004   240  .        .

 

data learn.manyper;
set learn.oneper;
array Dx{3};
do Visit = 1 to 3;
if missing(Dx{Visit}) then leave;
Diagnosis = Dx{Visit};
output;
end;
keep Subj Diagnosis Visit;
run;

Let’s take the time to describe in detail how this program works (feel free to skip this
section if this program seems intuitively clear to you). The DO loop starts with Visit set
equal to 1. The MISSING function tests if this value is missing. For Subject 001, none of
the diagnosis codes are missing, so the IF statement is never true for this subject. A new
variable, Diagnosis, is set equal to the value of the array element Dx{1}, which is the
same as the variable Dx1, which is equal to 450. At this point, the program data vector
(PDV) contains the following:
Subj Dx1 <drop> Dx2 <drop> Dx3 <drop> Visit Diagnosis
001  450              430              410                1   450
Because of the KEEP statement, only the variables Subj, Visit, and Diagnosis are written
out to data set Manyper at the bottom of the DO loop.

 

(The above is from my textbook , I just don't know when the variables DX2 and DX3 was read from the raw data ? Thank you . )

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

It appears that the variables Dx1 Dx2 and Dx3 appear in your data set learn.oneper if you are getting values for the Diagnosis variable.

 

The SAS SET statement, as Set learn.oneper; brings in the values from that data set.

If you have missing values for Diagnosis then that would mean that either 1) the values were missing in Learn.oneper or 2) the variables did not exist in Learn.oneper and the Array Dx{3} ; statement created them and they would have missing values as nothing is assigned to them, 3) possibly some combination of variables Dx existed in the set and the Array created the other one.

 

If you actually have datasets to run this on you should remove (or comment out the Keep statement so that it is not used). Commented out the code might look like:

data learn.manyper;
set learn.oneper;
array Dx{3};
do Visit = 1 to 3;
if missing(Dx{Visit}) then leave;
Diagnosis = Dx{Visit};
output;
end;
/* keep Subj Diagnosis Visit;*/
run;

Then see what your Learn.manyper set looks like.

 

Depending on what has gone before in class materials I would say that the description is a bit short because the behavior of the array statement is pretty complex all by itself. The Array statement can 1) reference existing variables, 2) create new variables, 3) reference existing and create new variables as well as possibly setting values for those variables.

View solution in original post

2 REPLIES 2
ballardw
Super User

It appears that the variables Dx1 Dx2 and Dx3 appear in your data set learn.oneper if you are getting values for the Diagnosis variable.

 

The SAS SET statement, as Set learn.oneper; brings in the values from that data set.

If you have missing values for Diagnosis then that would mean that either 1) the values were missing in Learn.oneper or 2) the variables did not exist in Learn.oneper and the Array Dx{3} ; statement created them and they would have missing values as nothing is assigned to them, 3) possibly some combination of variables Dx existed in the set and the Array created the other one.

 

If you actually have datasets to run this on you should remove (or comment out the Keep statement so that it is not used). Commented out the code might look like:

data learn.manyper;
set learn.oneper;
array Dx{3};
do Visit = 1 to 3;
if missing(Dx{Visit}) then leave;
Diagnosis = Dx{Visit};
output;
end;
/* keep Subj Diagnosis Visit;*/
run;

Then see what your Learn.manyper set looks like.

 

Depending on what has gone before in class materials I would say that the description is a bit short because the behavior of the array statement is pretty complex all by itself. The Array statement can 1) reference existing variables, 2) create new variables, 3) reference existing and create new variables as well as possibly setting values for those variables.

tianerhu
Pyrite | Level 9

Thank you for your help.

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
  • 477 views
  • 0 likes
  • 2 in conversation