DATA Step, Macro, Functions and more

Selective upcase

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

Selective upcase

I'd appreciate some advice regarding selective upcase.

Consider the following string:

"Some examples are Sas, Fbi, Cia, Fda, and Nsa."

Is there a practical way of saying "when the string contains ('Sas', 'Fbi', 'Cia', etc.) then those should be upcase?

I can always treat each individual case using the case function w/multiple when-then, but I'd rather put everything that should be upcase within a set of paranthesis, as above. If possible... Smiley Happy

Thanks for your time.


Accepted Solutions
Solution
‎05-05-2015 08:32 AM
Super User
Super User
Posts: 7,997

Re: Selective upcase

Posted in reply to EinarRoed

Yes, though I don't use formats.  As mentioned, first step would be to create the dataset of the pairs, e.g.

VAL1               VAL2

Bank 1 Asa      Bank 1 ASA

Bank 2 Asa      Bank 2 ASA

Then from that dataset you can do:

data _null_;

     set codelist end=last;

     if _n_=1 then call execute('data want; set have;');

     call execute('if myvalue="'||strip(val1)||'" then newvalue="'||strip(val2)||"';');

     if last then call execute('run;');

run;

Note, you could alo do the above by merging as well.  Its really a case of knowing what to change, i.e. what logic rules, up front, rather than a method of coding it.

View solution in original post


All Replies
Super User
Super User
Posts: 7,997

Re: Selective upcase

Posted in reply to EinarRoed

Well, if you can put some logic rules on it then possibly.  For instance, will they always be three letters?  I doubt you can put logic on it though, as what your talking about is anachronisms which aren't part of a basic language formula, but a made up thing by companies or such like.  For instance, Sas, could relate to SAS Software, the SAS (military), Scandanavian Air Services etc. depending on context.  Cia could be a shortening of Cioa in Italian etc.

So without a logical rule, you would need to supply a list of them yourself.  Maybe create a dataset with the ones you know, and a decode.  Then you can update that separately from your code.  E.g.

proc sql;

     create table WANT as

     select     A.*,

                  (select LONG_VAL from CODELIST where SHORT_VAL=A.SHORT_VAL) as LONG_VAL

     from      HAVE;

quit;

Dataset:

Short_Val     Long_val

fbi                FBI

sas               SAS

...

Frequent Contributor
Posts: 90

Re: Selective upcase

Thanks for the feedback. Here's a little more background:

We have a list of bank names, and they're all in upcase. We need them in propcase. However, there's a few 'shared terms' that should always be upcase, such as for example 'ASA'.

Basically I want to avoid writing this for each of those terms and banks:

case

     when 'Bank 1 Asa' then 'Bank 1 ASA'

     when 'Bank 2 Asa' then 'Bank 2 ASA'

     when 'Bank 3 Asa' then 'Bank 3 ASA'

     when 'Bank 4 Asa' then 'Bank 4 ASA'

end


I could of course use tranwrd(bankname, 'Asa', 'ASA'), but is there a way to use it multiple times, for 5-6 different terms, within the same expression? In example 'As' should always be 'AS', and 'Sb' should always be 'SB' - and only as long as they're individual words, not part of other words ('Asatru Asa' should not become 'ASAtru ASA', but 'Asatru ASA', to use a random fictive example).

Solution
‎05-05-2015 08:32 AM
Super User
Super User
Posts: 7,997

Re: Selective upcase

Posted in reply to EinarRoed

Yes, though I don't use formats.  As mentioned, first step would be to create the dataset of the pairs, e.g.

VAL1               VAL2

Bank 1 Asa      Bank 1 ASA

Bank 2 Asa      Bank 2 ASA

Then from that dataset you can do:

data _null_;

     set codelist end=last;

     if _n_=1 then call execute('data want; set have;');

     call execute('if myvalue="'||strip(val1)||'" then newvalue="'||strip(val2)||"';');

     if last then call execute('run;');

run;

Note, you could alo do the above by merging as well.  Its really a case of knowing what to change, i.e. what logic rules, up front, rather than a method of coding it.

Valued Guide
Posts: 860

Re: Selective upcase

Posted in reply to EinarRoed

you can use proc format for this.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 223 views
  • 3 likes
  • 3 in conversation