BookmarkSubscribeRSS Feed
RobertNYC
Obsidian | Level 7

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?

3 REPLIES 3
art297
Opal | Level 21

/*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.

Tom
Super User Tom
Super User

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;

MikeZdeb
Rhodochrosite | Level 12

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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 875 views
  • 0 likes
  • 4 in conversation