BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Emma2021
Quartz | Level 8
I have multiple variables as var1, var2,…var78, num1,num2 etc.
They all have labels. I want to change the labels by adding 3 strings : for example, for all variables with start var then add VAR at the beginning of the labels if num1 etc all variables with start num then add NUM at the beginning of the all labels. How can I do that? Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If the number of variables is small (or more accurately the total length of all of the new labels is small) enough to fit into a macro variable just use an SQL query to generate the code needed.

proc sql noprint;
select catx('=',nliteral(name),quote(catx(' ',substr(name,1,3),label),"'"))
  into :labels separated by ' '
  where libname = 'WORK'
    and memname='HAVE'
    and upcase(substr(name,1,3)) in ('VAR','NUM')
    and label ne ' '
  ;
quit;
proc datasets nolist lib=work;
  modify have ;
    label &labels;
  run;
quit;

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

If the number of variables is small (or more accurately the total length of all of the new labels is small) enough to fit into a macro variable just use an SQL query to generate the code needed.

proc sql noprint;
select catx('=',nliteral(name),quote(catx(' ',substr(name,1,3),label),"'"))
  into :labels separated by ' '
  where libname = 'WORK'
    and memname='HAVE'
    and upcase(substr(name,1,3)) in ('VAR','NUM')
    and label ne ' '
  ;
quit;
proc datasets nolist lib=work;
  modify have ;
    label &labels;
  run;
quit;
Emma2021
Quartz | Level 8
If too long—can we stratify by var variables and num variables? How can do that? Thank you so much.
AhmedAl_Attar
Ammonite | Level 13

Hi @Emma2021 

 

One wat to get around the potential "too long" scenario would be writing the code into a separate file, and then running the newly created file 😉

Basically take @Tom original code and re-arrange it slightly

filename dyncode temp;

data _null_;
	length str $300;
	file dyncode lrecle=300;
	put 'proc datasets nolist lib=work;' ;
	put +3 'modify have ;' ;
	put +3 'label ';
	do until(eof);
		SET sashelp.vcolumn(where=(libname='WORK' and memname='HAVE' and upcase(substr(name,1,3)) in ('VAR','NUM') and label ne ' ')) 
			end=eof;
		str = catx('=',nliteral(name),quote(catx(' ',substr(name,1,3),label),"'"));
		put +6 str;
	end;	
	put +3 ';';
	put 'run; quit; ';
run;
%include dyncode;

The generated code should be a single Proc Datasets step.

 

Hope this help

Emma2021
Quartz | Level 8
It gives an error that no from statement and no found name variable
AhmedAl_Attar
Ammonite | Level 13
insert
FROM dictionary.columns
above
Where libname = 'WORK'
yabwon
Onyx | Level 15

You already have a solution, but just for fun, an alternative one with proc transpose and call execute():

data have;
  retain var1 - var78 "abc" num1 - num20 0 A B C D E F G "xxx";
  ATTRIB
    var1 -- var78 label= "some label"
    num1 -- num20 label= "other lable"
    A -- G        label= 'different variables'
  ;
run;

proc transpose data=have(obs=0) out=temp;
  var var: num:;
run;
data _null_;
  call execute('
    proc datasets nolist lib=work;
      modify have ;
        label');
  do until(eof);
    set temp end=eof;
    call execute(cat(_NAME_,"='",substr(_NAME_,1,3)," ",_LABEL_,"'"));
  end;

  call execute(';run;quit;');
stop;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 1093 views
  • 6 likes
  • 4 in conversation