- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You need to use the CATX function. It takes care of missing values.
SITE ADD= catx(' '
,Street_number
,Street_name
,Street_type
,Suburb
,Postcode);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You need to use the CATX function. It takes care of missing values.
SITE ADD= catx(' '
,Street_number
,Street_name
,Street_type
,Suburb
,Postcode);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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[*])
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Did you see this reply?
SITEADD= catx( of MAYARRAY[*] );
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;