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;
You need to use the CATX function. It takes care of missing values.
SITE ADD= catx(' ' ,Street_number ,Street_name ,Street_type ,Suburb ,Postcode);
You need to use the CATX function. It takes care of missing values.
SITE ADD= catx(' ' ,Street_number ,Street_name ,Street_type ,Suburb ,Postcode);
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
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[*])
Did you see this reply?
SITEADD= catx( of MAYARRAY[*] );
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 ..
If it's too much of a bother please ignore my reply.
But many thanks to all for the quick turn around
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.