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

I had tried many codes but didn't get solution help me to find out solution

 

data m;
input v1$ v2$ v3$ v4$;
cards;
v  1   .   2
.   2  b  f
.    .  4  6
2  .   .   9
;
run;

Need output like this 
1
b
6
9

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Rules.

Computer programs are rules.

I can make your result. But it will not work for anything other than those exact values because you have not provided an rules related to how which value is selected on each row.

If the rule is "second non-missing value" then one way

data want;
  set m;
  length value $ 8;
   value = scan(catx(',',of v:),2,',');
run;

which builds a list of values separated by commas with CATX. The Catx function remove trailing blanks so the missing values aren't included when the commas are inserted. The Scan gets the second value.

Note: if your values actually include a comma then use a different character in the first parameter for Catx and the last in Scan.

View solution in original post

4 REPLIES 4
Quentin
Super User

What is the logic rule for selecting the output value?  It's not the first, not the last, not the first letter... I can't see what the rule is.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
ballardw
Super User

Rules.

Computer programs are rules.

I can make your result. But it will not work for anything other than those exact values because you have not provided an rules related to how which value is selected on each row.

If the rule is "second non-missing value" then one way

data want;
  set m;
  length value $ 8;
   value = scan(catx(',',of v:),2,',');
run;

which builds a list of values separated by commas with CATX. The Catx function remove trailing blanks so the missing values aren't included when the commas are inserted. The Scan gets the second value.

Note: if your values actually include a comma then use a different character in the first parameter for Catx and the last in Scan.

112211
Obsidian | Level 7
Use coalescec function or mixed of coalescec and other functions
ballardw
Super User

@112211 wrote:
Use coalescec function or mixed of coalescec and other functions

Still not a rule on selecting which values and COALESCE functions select the first that appear in a list of values. So obviously not the the choice for the given example.

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 956 views
  • 0 likes
  • 3 in conversation