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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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: 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
  • 4 replies
  • 877 views
  • 0 likes
  • 3 in conversation