Help using Base SAS procedures

Assigning labels to a large amount of variables

Reply
Frequent Contributor
Posts: 101

Assigning labels to a large amount of variables

I have a dataset with about 500 variables. The all startwith the prefix “ftc”. So, ftc1, ftc2, ftc3, ftc4…. I have to assign a label to each one removing the “ftc” and replacing it with a “Q” so Q1, Q2, Q3, Q4. Isthere a ay to do this to a very large group of variables at once?

PROC Star
Posts: 7,485

Re: Assigning labels to a large amount of variables

Posted in reply to RobertNYC

/*create test file*/

data have;

  retain ftc1-ftc500 (500*1);

  output;

run;

data want;

  set have (rename=(ftc1-ftc500=q1-q500));

run;

Now that I have also seen my colleague's responses, I have to wonder if I correctly interpreted what you wanted.  My approach was simply renaming the variables. Is that what you wanted to or, as in your subject line, did you actually want to assign labels?

If it is the latter, please disregard my response.

Super User
Super User
Posts: 7,062

Re: Assigning labels to a large amount of variables

Posted in reply to RobertNYC

Code gen.

- If you have a good editor you could just do it in the editor.  If I still had WYLBUR I could do it with a couple of change commands.

- Macro logic.

- Data step logic to write to a file.

- Proc sql to store code in a macro variable.

Once youve generated the variable labels pairs then you also have options on how to apply them.

- DATA step to create a new (or overwrite the old) dataset by reading the existing one.

- PROC SQL code to do the same.

- PROC DATASETS MODIFY code to just modify the labels of the variables without having to create (re-create) the dataset.

filename code temp;

data _null_;

  file code;

  put 'label ';

  do i=1 to 500;

    put 'ftc' i '="Q' i '"' ;

  end;

  put ';';

run;

data new;

  set old;

%inc code;

run;

Valued Guide
Posts: 765

Re: Assigning labels to a large amount of variables

Posted in reply to RobertNYC

hi ... another idea

data x;

retain ftc1-ftc100 1 a b c 'other';

run;

proc sql noprint;

select catt(name, '= "Q' , compress(name,,'kd'),'"') into :labels separated by ' '

from dictionary.columns where libname eq 'WORK' and memname eq 'X' and name eqt 'ftc';

quit;

proc datasets lib=work nolist;

modify x;

label &labels;

quit;

%symdel labels;

Ask a Question
Discussion stats
  • 3 replies
  • 150 views
  • 0 likes
  • 4 in conversation