turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- SAS programming: select a numerical value with the...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-02-2017 06:58 PM - edited 04-02-2017 07:50 PM

Hi guys!

Please help me!

I have 106 subjects/observations and 16 numerical variables (var1-var16) with a date corresponding to each numerical variable (date1-date16). The date variables (and their corresponding numerical variables) are ordered so that older dates are placed last for each subject (e.g. subject 1 may have values in all 16 numerical and date variables with 16 being the oldest, subject 2 may only have 8 numerical and date variables with 8 being the oldest).

Now, I need to define a baseline_var that correspond to the numerical value with the oldest date for each subject. How can I program SAS to find this baseline value?

Thank you!

Accepted Solutions

Solution

04-02-2017
10:58 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

04-02-2017 10:00 PM

Arthur.T,

Use function coalesce(),no need array.

```
data have;
infile cards truncover;
input var1-var16;
cards;
1 2 5 7 6
2 2 4 5
3 4 2 7 9
9 8 7 6 5 3 2 8 1
;
data want;
set have;
want=coalesce(of var16-var1);
run;
```

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michan22

04-02-2017 08:01 PM

```
date_min = min(of date1-date16);
format date_min date9.l
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michan22

04-02-2017 08:17 PM

Assuming no ties, and this is untested.

You can find the largest value using the MAX function and then the corresponding index using WHICHN. I've nested these, but you may need to separate out the calculations.

If you know that the last record is the largest and missing values are used for the renaming values you can also count how many missing values are present (NMISS). Then the largest index would be something like the commented out option.

```
array vars(*) var1-var16;
array dates(*) date1-date16;
index_largest = whichn(max(of dates(*)), of dates(*));
```

*index_largest = dim(vars) - nmiss(of vars(*));

*index_largest = 16 - nmiss(of vars(*));
value = vars(index_largest);

michan22 wrote:

Hi guys!

Please help me!

I have 106 subjects/observations and 16 numerical variables (var1-var16) with a date corresponding to each numerical variable (date1-date16). The date variables (and their corresponding numerical variables) are ordered so that older dates are placed last for each subject (e.g. subject 1 may have values in all 16 numerical and date variables with 16 being the oldest, subject 2 may only have 8 numerical and date variables with 8 being the oldest).

Now, I need to define a baseline_var that correspond to the numerical value with the oldest date for each subject. How can I program SAS to find this baseline value?

Thank you!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

04-02-2017 08:49 PM

Obviously, a number of us are interpreting your question differently. I think the following is what you want:

data have; infile cards truncover; input var1-var16; cards; 1 2 5 7 6 2 2 4 5 3 4 2 7 9 9 8 7 6 5 3 2 8 1 ; data want; set have; array nums(*) var1-var16; do _n_=dim(nums) to 1 by -1; if not missing(nums(_n_)) then do; want=nums(_n_); leave; end; end; run;

Art, CEO, AnalystFinder.com

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michan22

04-02-2017 09:35 PM

Hi guys! thanks for all the advice!

Maybe this will clear up my question. So my dateset looks like this:

var1 date1 var2 date2 var3 date3 var4 date4 ....... var16 date16

200 08/09/16 300 05/02/16 400 06/10/15 500 01/20/15 ....... 350 02/15/05

250 07/06/16 200 04/10/14

.

.

.

so for the 1st observation my baseline would be 350 because that's value corresponding to the oldest date, and 2nd observation would be 200 because that correspond to the oldest date.

I want the numerical value that correspond to the oldest date to be the baseline and I need to find this baseline for each subject.

Thank you!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to michan22

04-02-2017 09:38 PM

That's precisely what the code I suggested does. The date variables are irrelevant as long as they are in date order.

Art, CEO, AnalystFinder.com

Solution

04-02-2017
10:58 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

04-02-2017 10:00 PM

Arthur.T,

Use function coalesce(),no need array.

```
data have;
infile cards truncover;
input var1-var16;
cards;
1 2 5 7 6
2 2 4 5
3 4 2 7 9
9 8 7 6 5 3 2 8 1
;
data want;
set have;
want=coalesce(of var16-var1);
run;
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

04-02-2017 10:58 PM

Oh my gosh thank you! this is life changing!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

04-03-2017 12:22 AM

By the way, why is this function called coalesce? Just out of curiosity so it will help me to remembering it more easier.

Thanks!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to wong

04-03-2017 06:08 AM

I don't know. Maybe read it literally .

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to wong

04-03-2017 08:22 AM

My guess is that it comes from the original definition in computer science, namely: the merging of adjacent blocks of memory to fill gaps caused by deallocated memory

Art, CEO, AnalystFinder.com