BookmarkSubscribeRSS Feed
jing2000yr
Calcite | Level 5

Hi,

I am a new user ask for help. Attached the sample data, I have four variables from 4 visits which acutually measured the same biological marker. Subjects were measured in one or two visits but not all, so the Var 1-4 had missing values. I want to build a new variable, by selecting one from the four variables.

1. How to select from the first available data in the four visits to build the new one?

2. How to select the last available data in the four visits to build the new one?

Thanks!

JingScreenshot 2023-10-10 102037.png

3 REPLIES 3
Kurt_Bremser
Super User

You seem to have an Excel issue. You posted a screenshot of an Excel spreadsheet, and attached an Excel file.

I recommend that you post your question at an Excel-oriented forum, this here are the SAS communities.

antonbcristina
SAS Employee

Considering you might want to solve this in SAS, the following will work. You want to use an array to iterate through the available measures (var1-var4). You will assign first to be the first non-missing value and assign last to be the last non-missing value.

 

data want;
    set have;
    array visits [*] var1-var4;
    do i = 1 to dim(visits);
        if first=. then first=visits[i];       
        if visits[i]^=. then last=visits[i];
    end;
run;

 

 

Tom
Super User Tom
Super User

Use the COALESCE() function to select the first non-missing value from a list.  Since your variable names have numeric suffixes you can use a variable list.

first = coalesce(of var1-var4);

Otherwise just list the variable names:

first = coalesce(of varA varB varC varD);

To find the last non-missing value just list the variables in the opposite order.

last = coalesce(of var4-var1);

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 700 views
  • 3 likes
  • 4 in conversation