How do I upcase all but one character variable

Accepted Solution Solved
Reply
Occasional Contributor _AB
Occasional Contributor
Posts: 5
Accepted Solution

How do I upcase all but one character variable

So I want to convert all the character variables to Uppercase excluding the Customer_ID (which is also character). I use an array to convert all the character values to Uppercase but because of this one ID variable I am stuck. I have to do this for multiple data sets. Any kind of help is greatly appreciated. Thank you.


Accepted Solutions
Solution
‎08-24-2012 10:12 AM
Respected Advisor
Posts: 4,920

Re: How do I upcase all but one character variable

This test works just fine :

data test;
v1 = 1;
v2 = "a string";
customer_id = "1233a";
v3 = "another string";
run;

data test1;
    set test;
    array char(*) _character_;
    do i = 1 to dim(char);
        if upcase(vname(char{i})) ne "CUSTOMER_ID" then char{i} = upcase(char{i});
    end;
    drop i;
run;

proc print;
run;

                                         customer_
                Obs    v1       v2          id              v3

                 1      1    A STRING      1233a      ANOTHER STRING

PG

PG

View solution in original post


All Replies
Respected Advisor
Posts: 4,920

Re: How do I upcase all but one character variable

Use

if upcase(vname(myArray{i})) ne "CUSTOMER_ID" then myArray{i} = upcase(myArray{i});

PG

PG
Occasional Contributor _AB
Occasional Contributor
Posts: 5

Re: How do I upcase all but one character variable

Thanks PG, that looks great but what elements should I define the array with? I have both character and numeric variables in my data set. When I use _all_ it gives an error and when I use _character_ it changes all the character variable uppercase including the CUSTOMER_ID. I can't just specify variables from the data set because this code will do inside a macro which will be applied to all the data sets in the library.

this is my code right now,

data cbt.test1;

    set cbt.test;

    array char(*) _character_;

    do i = 1 to dim(char);

        if upcase(vname(char{i})) ne "Customer_ID" then char{i} = upcase(char{i});

    end;

    drop i;

run;

thanks again

AB

Respected Advisor
Posts: 4,920

Re: How do I upcase all but one character variable

You must change "Customer_ID" to "CUSTOMER_ID" for the comparison to work properly.

PG

PG
Occasional Contributor _AB
Occasional Contributor
Posts: 5

Re: How do I upcase all but one character variable

Sorry that was just me typing carelessly, I have made sure that there are no syntax errors. It changes every character valuer uppercase. I tested it with multiple character variables.

PROC Star
Posts: 7,468

Re: How do I upcase all but one character variable

Do a proc contents of the original file and check the name of your supposed "customer_id" variable.  My guess is that you have the name wrong as PGStat's code appears to work as intended.

Solution
‎08-24-2012 10:12 AM
Respected Advisor
Posts: 4,920

Re: How do I upcase all but one character variable

This test works just fine :

data test;
v1 = 1;
v2 = "a string";
customer_id = "1233a";
v3 = "another string";
run;

data test1;
    set test;
    array char(*) _character_;
    do i = 1 to dim(char);
        if upcase(vname(char{i})) ne "CUSTOMER_ID" then char{i} = upcase(char{i});
    end;
    drop i;
run;

proc print;
run;

                                         customer_
                Obs    v1       v2          id              v3

                 1      1    A STRING      1233a      ANOTHER STRING

PG

PG
Occasional Contributor _AB
Occasional Contributor
Posts: 5

Re: How do I upcase all but one character variable

Thank you. It worked. I was unaware that the CUSTOMER_ID has to be uppercase as well although it is not in actual variable.

AB

Respected Advisor
Posts: 3,799

Re: How do I upcase all but one character variable

I have often wondered how much performance is affected by the V functions, in this case were we keep asking the same question but the answer can never change.  I have never tested it.

data test1;

   link IC;

   do until(eof);

      set test end=eof;

      array char(*) _character_;    

      do i = 1 to ic-1, ic+1 to dim(char);

         char{i} = upcase(char{i});

         end;

      output;

      end;

   stop;

  

IC:

   do ic = 1 to dim(char) until(upcase(vname(char{ic})) EQ "CUSTOMER_ID"); end;

   return;

   run;

Respected Advisor
Posts: 4,920

Re: How do I upcase all but one character variable

A simple and efficient solution to avoid the V functions is :

data test1;

     set test;

     array char{*} _character_;

     dum = Customer_Id;

     do i = 1 to dim(char);

         char{i} = upcase(char{i});

         end;

    Customer_Id = dum;

    drop i dum;

run;

PG

PG
Respected Advisor
Posts: 3,799

Re: How do I upcase all but one character variable

I wasn't really singling out the V functions but more the fact that you called repeatedly for to answer a question that can never change.

Your use of a dummy variable falls into the same category, doing work that is unnecessary.  Although I'm pretty sure the difference is small and doen't really matter much.

Super User
Posts: 5,498

Re: How do I upcase all but one character variable

Posted in reply to data_null__

You can also get rid of the VNAME function (as well as the UPCASE function) if you make sure Customer_ID is the first character variable.  For example:

data want;

   if 0 then set have (keep=customer_ID);

   set have;

   array char{*} _character_;

   do _n_=2 to dim(char);

       char{_n_} = upcase(char{_n_});

   end;

run;

A LENGTH statement would do the trick, but this way you don't need to know the length.

Respected Advisor
Posts: 3,799

Re: How do I upcase all but one character variable

Posted in reply to Astounding

Good idea.  Since customer_id is probably a key and probably the first column anyway and if not I might be nice to have it in the first column anyway.....

Respected Advisor
Posts: 4,920

Re: How do I upcase all but one character variable

Posted in reply to Astounding

, I would suggest that Outstanding"s code is the best answer to your question. It is efficient and simple and can be extended to more than one variable.

PG

PG
Occasional Contributor _AB
Occasional Contributor
Posts: 5

Re: How do I upcase all but one character variable

Thanks and and everyone else. I learned quite a few things. I will contribute as much as possible to the community.

|AB

🔒 This topic is solved and locked.

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

Discussion stats
  • 17 replies
  • 565 views
  • 4 likes
  • 5 in conversation