BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
nwang5
Obsidian | Level 7

Is there any easy to express the sentences below?

 

if lle9^=. then age_baseline=R9AGEY_B;
else if lle10^=. then age_baseline=R10AGEY_B;
else if lle11^=. then age_baseline=R11AGEY_B;
else if lle12^=. then age_baseline=R12AGEY_B;
else if lle13^=. then age_baseline=R13AGEY_B;
else if lle14^=. then age_baseline=R14AGEY_B;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Define "easy".

I can do it in slightly shorter code but whether it is easier or not is questionable.

Untested code as no values provided

 

array ab (*) R9AGEY_B R10AGEY_B R11AGEY_B R12AGEY_B R13AGEY_B R14AGEY_B;
baseline = ab [whichn(1,lle9^=. ,lle10^=.,lle11^=.,lle12^=.,lle13^=.,lle14^=.)];

The Whichn, and character version Whichc, returns the position of the first expression or value following the first that matches that value. So we find the first result of 1 from the comparisons. SAS will return 1 for true so evaluating lle9 ^=. results in 1 when not missing.

 

The ARRAY allows addressing a value of the array using it's position number. This is where the bit about middle of the variable comes in. You will learn that with SAS it often a much better idea to name the variable R_agey_b9, or similar. Then the list could have been R_agey_b9 -R_agey_b14. 

For the above to work you have to make sure that the order of the comparisons matches that of the variables in the array.

 

Or a two array solution

array ab (*) R9AGEY_B R10AGEY_B R11AGEY_B R12AGEY_B R13AGEY_B R14AGEY_B;
array l (*) lle9 - lle14;
do i=1 to dim(l);
   if l[i] ne . then do;
      baseline=ab[i];
      leave;
   end;
run;

 

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Define "easy".

I can do it in slightly shorter code but whether it is easier or not is questionable.

Untested code as no values provided

 

array ab (*) R9AGEY_B R10AGEY_B R11AGEY_B R12AGEY_B R13AGEY_B R14AGEY_B;
baseline = ab [whichn(1,lle9^=. ,lle10^=.,lle11^=.,lle12^=.,lle13^=.,lle14^=.)];

The Whichn, and character version Whichc, returns the position of the first expression or value following the first that matches that value. So we find the first result of 1 from the comparisons. SAS will return 1 for true so evaluating lle9 ^=. results in 1 when not missing.

 

The ARRAY allows addressing a value of the array using it's position number. This is where the bit about middle of the variable comes in. You will learn that with SAS it often a much better idea to name the variable R_agey_b9, or similar. Then the list could have been R_agey_b9 -R_agey_b14. 

For the above to work you have to make sure that the order of the comparisons matches that of the variables in the array.

 

Or a two array solution

array ab (*) R9AGEY_B R10AGEY_B R11AGEY_B R12AGEY_B R13AGEY_B R14AGEY_B;
array l (*) lle9 - lle14;
do i=1 to dim(l);
   if l[i] ne . then do;
      baseline=ab[i];
      leave;
   end;
run;

 

 

 

andreas_lds
Jade | Level 19

This "solution" demonstrates how to avoid loops:

options missing = '#';

data want;
   set have;

   length value $ 10 all $ 200 p baseline 8;

   array one lle9-lle14;
   array two R9AGEY_B R10AGEY_B R11AGEY_B R12AGEY_B R13AGEY_B R14AGEY_B;

   value = put(coalesce(of one[*]), best.);
   all = catx(' ', of one[*]);
   p = findw(all, strip(value), ' ', 'e');

   baseline = two[p];

   *drop value all p;
run;

options missing = '.';

While coalesce still sees "missing", catx keeps missing values and displays them as #, so findw returns the number of the "word" matching the first non-missing value, which is the value of array two becoming the value of baseline.

 

In real-life i would use the second suggestion posted by @ballardw 

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
  • 365 views
  • 0 likes
  • 3 in conversation