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;
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;
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;
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.