BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
noda6003
Quartz | Level 8

I want to concatente 3 variables and if i have missing varaible at end than want to replace it with "Y".

Can anyone help me how to do it?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

good attempt @noda6003  just a tweak-

data have;
 input var1-var3;
 cards;
1          2            .

1          3           2

2          1           3
;

data want;
 set have;
 length want $10;
 want=catx('/',var1,var2,ifc(var3=.,'Y',put(var3,8.)));
run;

View solution in original post

8 REPLIES 8
novinosrin
Tourmaline | Level 20

Hi @noda6003  Can you please post a sample of HAVE and WANT(expected output) explaining the logic?

noda6003
Quartz | Level 8

have

col1    col2       col3

1          2            .

1          3           2

2          1           3

 

want

col4

1/2/Y

1/3/2

2/1/3

   

Tom
Super User Tom
Super User

The CAT... functions will honor the MISSING option setting.

data have ;
  input col1-col3 ;
cards;
1          2            .
1          3           2
2          1           3
;

options missing='Y';
data want;
  set have;
  want=catx('/', of col1-col3);
run;
options missing='.';

proc print;
run;
Obs    col1    col2    col3    want

 1       1       2       .     1/2/Y
 2       1       3       2     1/3/2
 3       2       1       3     2/1/3
me55
Quartz | Level 8

the easiest way to do what you are doing is to update the table to y where value=null. to concantenate, you would want to use the cat function of which there are many varieties like cat-s, cat-t...here is a webpage on using the cat function...

 

SAS CAT-S Function 

 

an example would be...

 

data want;

  data have;

  new_var=catx('',var1,var2,var3);

run;

 

that is the data step version.  i work more in proc sql.  i can give you an example of that if you want but that should work.  

 

 

Amir
PROC Star

Hi @noda6003 ,

 

As @novinosrin has requested, it really helps to see the data, including data types.

 

In principle, the following logic might be a good starting point:

 

/* set up data in "have" data set */
data have;
   infile datalines truncover;

   input
      var1 : $char1.
      var2 : $char1.
      var3 : $char1.
   ;

   datalines;
a b c
d e  
g h i
;

/* create data in "want" data set based on "have" data set */
data want;
   set have;
   length result $ 3;

   /* if var3 is missing then use "Y" otherwise use var3 */
   result = cats(var1,var2,ifc(missing(var3),'Y',var3));
run;

The processing in the 2nd data step should also work for numeric input variables.

 

If this is not what you want then please provide example data and logic as requested.

 

 

Kind regards,

Amir.

ballardw
Super User

Please show the code you are using to concatenate the variables.

I suspect you can replace one of the variables with : coalescec(varname,'Y')

 

The coalescec function returns the first non-missing value in a list of character values be they variables or literals.

Coalesce does the same thing for numeric values.

noda6003
Quartz | Level 8

I use  ct = cats("/",col1,col2,ifc(missing(col3),'Y',col3));

 

But it does not work

novinosrin
Tourmaline | Level 20

good attempt @noda6003  just a tweak-

data have;
 input var1-var3;
 cards;
1          2            .

1          3           2

2          1           3
;

data want;
 set have;
 length want $10;
 want=catx('/',var1,var2,ifc(var3=.,'Y',put(var3,8.)));
run;

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
  • 8 replies
  • 1990 views
  • 0 likes
  • 6 in conversation