BookmarkSubscribeRSS Feed
Doyleuk
Calcite | Level 5
Hi,

I was wondering if anyone can supply code that allows me to UPCASE all variables in a dataset.

Currently i have a macro which loops through each of the names and upcases them 1 at a time in a data step.

I was hoping there was an easier way to do this.

Like so

proc datasets library = cwork nodetails ;
contents noprint data = &final_name out = work.test3 ;
run;quit;

proc sql noprint;
select Count(*)
into :No_obs
from test3;
select NAME
into :KEEP_VAR1-:KEEP_VAR%left(&No_obs)
from test3;
quit;


%do k=1 %to &No_obs;

%let Keep_Var1 = &Keep_Var&k;

data cwork.&final_name;
set cwork.&final_name;
&Keep_Var1 = upcase(&Keep_Var1);
run;
10 REPLIES 10
LinusH
Tourmaline | Level 20
There are no syntax available to my knowledge that will do this.
But I think you are on the right track.
You could either in your SQL and perhaps by subsequent string manipulation prepare UPCASE assignments for all variables,
or do your macro loop inside your data step so you don't have to rewrite your SAS table for each variable to upcase.

/Linus
Data never sleeps
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
From what you have explained, it appears that you want to UPCASE the variable "values", not the variable "names" in your SAS datasets. You can use a DATA step with an ARRAY and reference _CHARACTER_ as the list of variables in the ARRAY. Then code a DO I=1 TO DIM(); END; and do your UPCASE re-assignment.

You will find useful topic-oriented information at the SAS support http://support.sas.com/ website, using the SEARCH facility.

Scott Barry
SBBWorks, Inc.

SAS 9.2 DOC: Array Processing
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a002299816.htm


Variations on Basic Array Processing
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a000739610.htm
Florent
Quartz | Level 8
Hi,

Here is a sample code I created to do the job 😉
You just need to update the parameters which are passed into the macro call.

Enjoy !

Kind regards,
Florent



%MACRO UpcaseCharVar(lib, ds); /* lib: library's name
ds: dataset's name */
proc sql noprint;
select count(distinct name)
into :NbrCharVar
from dictionary.columns
where libname = upcase("&lib")
and memname = upcase("&ds")
and type = 'char';
quit;

%let NbrCharVar = &NbrCharVar; /* Removes Leading and Trailing Blanks */

proc sql noprint;
select name
into :CharVar1-:CharVar&NbrCharVar
from dictionary.columns
where libname = upcase("&lib")
and memname = upcase("&ds")
and type = 'char';
quit;

%IF &NbrCharVar > 0 %THEN
%DO i=1 %TO &NbrCharVar;
proc sql;
update &lib..&ds
set &&CharVar&i = upcase(&&CharVar&i);
quit;
%END;
%MEND;

%UpcaseCharVar(Work, Test_DS);
Privet3711
Calcite | Level 5

Macro perfoms slow on large datasets with large number of char variables

I modified it

%MACRO UPCASE_ALL(LIB, DS); 

     PROC SQL NOPRINT;
     SELECT STRIP(NAME)||" = UPCASE( "||STRIP(NAME) || ");"
     INTO :CODE_STR
    SEPARATED BY ' '
     FROM DICTIONARY.COLUMNS
     WHERE LIBNAME = UPCASE("&LIB")
     AND MEMNAME = UPCASE("&DS")
     AND TYPE = 'char';
     QUIT;

DATA &DS;
SET  &DS;
  &CODE_STR
RUN;

%MEND;


%UPCASE_ALL(WORK,INCD2);

Doyleuk
Calcite | Level 5
Many Thanks.

Your code worked a treat
Howles
Quartz | Level 8

No need for a macro. Try this model.

data have ;

set sashelp.class ;

* keep _numeric_ ;

run ;

data want ;

set have ;

array charvar

  • _character_ ;
  • do i = 1 to dim(charvar) ; drop i ;

       charvar(i) = upcase( charvar(i) ) ;

       end ;

    run ;

    Note that if there happen to be no character variables, a WARNING appears. You may want to bulletproof for that situation.

    Tom
    Super User Tom
    Super User

    Here are a couple of tricks to make the code shorter (and in my opinion clearer).

    To eliminate the need to drop the index variable you can use the special variable _N_.

    data want ;

      set have ;

      array _char _character_;

      do _n_ =1 to dim(_char);

        _char(_n_)=upcase(_char(_n_));

      end;

    run;

    Or use the DO OVER syntax.  This is clearer for this type of problem as the index has no meaning for this array.

    data want ;

      set have ;

      array _char _character_;

      do over _char ;

        _char=upcase(_char);

      end;

    run;

    You could add and drop a character variable to prevent the warning.

    data want ;

      length _dummych $1;

      drop _dummych;

      set have ;

      array _char _character_;

      do _n_ =2 to dim(_char);

        _char(_n_)=upcase(_char(_n_));

      end;

    run;

    MikeZdeb
    Rhodochrosite | Level 12

    hi ... another possibilty (if you don't mind changing the variable names to uppercase in the process) ...

    * from ... http://support.sas.com/documentation/cdl/en/nlsref/61893/HTML/default/viewer.htm#a002473805.htm;

    proc trantab table=ascii;

    replace 'a' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    save table=upper;

    quit;

    data test;

    input name : $10. Gender : $1.  CITY : $10. age;

    datalines;

    Mike m Albany 25

    Art m Toronto 30

    ;

    proc cport data=test file='z:\test.cpt';

    trantab name=upper;

    run;

    proc cimport data=test_new file='z:\test.cpt';

    run;

    proc print data=test_new;

    run;

    Obs    NAME    GENDER     CITY      AGE

    1     MIKE      M       ALBANY      25

    2     ART       M       TORONTO     30

    art297
    Opal | Level 21

    Mike, Nice approach and this time I  definitely can follow the logic.  I thought you might have been referring to you and me in the example data .. until I saw the ages listed Smiley Happy

    Ksharp
    Super User

    Mike . You do not need to make translate table.

    In proc cport ,there is already an option to do this.

    filename tran 'c:\x.dat';
    proc cport library=sashelp outtype=upcase file=tran memtype=data;
     select class;
    run;
    proc cimport infile=tran library=work;run;
    
    
    

    Ksharp

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    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
    • 10 replies
    • 25044 views
    • 2 likes
    • 10 in conversation