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
PROC Star

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.

Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.
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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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