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

Hi All, I have 5 variables as below 

 

Street_number

Street_name

Street_type

Suburb

Postcode

 

I have writte a logic that checks if any of the variable names are  empty and then I concat the remaining fields that are not empty.

For that I have written multiple IF then logic's. Can I simplify the code like do this in a loop and reduce the IF Then's ?

********************************************************************

 

If street_number  eq ' ' and street_name not in  ' '  and street_type not in ( ' ') and suburb not in  ' ' and postcode not in   ' ' then

Siteadd =  trim(street_name) || ' ' || trim(street_type) ||' ' || trim(suburb ) || ' ' ||  trim(postcode )

Else if 

street_number  eq ' '  and street_name  in (' ') and street_type not in ( ' ') and suburb not in (' ') and postcode not in  (' ')

Then

Siteadd =   trim(street_type)|| ' '|| trim(suburb ) || ' ' ||  trim(postcode )

else if street_number  eq ' '  and street_name  in (' ') and street_type not in ( ' ') and suburb not in (' ') and postcode in (' ')

then siteadd= trim(street_type) ||  suburb;

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

You need to use the CATX function. It takes care of missing values.

SITE ADD= catx(' '
              ,Street_number
              ,Street_name
              ,Street_type
              ,Suburb
              ,Postcode);

 

View solution in original post

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

You need to use the CATX function. It takes care of missing values.

SITE ADD= catx(' '
              ,Street_number
              ,Street_name
              ,Street_type
              ,Suburb
              ,Postcode);

 

dennis_oz
Quartz | Level 8

thanks for the reply .. I will test your solution..

 

Can you please suggest how I can do this using arrays 

 

for example : 

 

and loop through 

 

Array Adress_variables(*) Stnum stname sttype Suburb postcode 

Patrick
Opal | Level 21

@dennis_oz 

There is no need for looping when using CATX(). May be read up in the documentation what this function provides.

 

You can use an array name instead of spelling out all the variables using syntax like:

catx(of myarray[*])

ChrisNZ
Tourmaline | Level 20

Did you see this reply?

 

SITEADD= catx( of MAYARRAY[*] );

 

 

dennis_oz
Quartz | Level 8

Hi Thanks , I have tested the catx solution and it works ..

 

I wanted to know if this could be done with arrays and hence i had asked the question Smiley Happy ..

If it's too much of a bother please ignore my reply.

 

But many thanks to all for the quick turn around

Patrick
Opal | Level 21

@dennis_oz 

Using catx() is just so much simpler. But here populating addr_comb_2 the way you could have done it in the old days before the strip() and catx() function got introduced.

data have;
  Street_number='Street_number';
  Street_name='Street_name';
  Street_type='      ';
  Suburb='Suburb';
  Postcode='Postcode';
  output;
  stop;
run;

data want(drop=_:);
  set have;
  length addr_comb_1 addr_comb_2 $200;
  array addrvars {*} Street_number Street_name Street_type Suburb Postcode;
  addr_comb_1=catx(' ', of addrvars [*]);
  do _i=1 to dim(addrvars);
    if addrvars[_i] ne ' ' then 
      addr_comb_2=trim(left(addr_comb_2))||' '||left(addrvars[_i]);
  end;
run;

proc print;
run;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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