BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi Everyone,

 

I have 2 dataset that have similar variables. One file is for daily and the other file is for monthly.

I want to add a term say "_daily" after all variables in daily file and "_monthly" for those in monthly file.

Is there any way to do so instead of using the rename one by one?

 

Thank you.

 

HC

 

data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;


data _null_;
 set sashelp.vcolumn(where=(libname='WORK' and memname='HAVE') keep=libname memname name) end=last;
 if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify have;rename');
 call execute(cats(name,'=',name,'_daily'));
 if last then call execute(';quit;');
run;

View solution in original post

11 REPLIES 11
Tom
Super User Tom
Super User

Use metadata (SASHELP.VCOLUMN or DICTIONARY.COLUMNS or output of PROC CONTENTS) to generate the rename code.

proc sql noprint ;
  select catx('=',name,cats(name,'_monthly'))
    into :monthly separated by ' '
    from dictionary.columns
    where libname='WORK'
      and memname='MONTHLY'
  ;
quit;
data want ;
   set monthly (rename=(&monthly)) ;
run;
Reeza
Super User

Slight modification to Tom's code - I would suggest using proc datasets to modify the variable names instead. 

If you're merging the files though, do it in that step instead of a separate rename step. 

Ksharp
Super User
data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;


data _null_;
 set sashelp.vcolumn(where=(libname='WORK' and memname='HAVE') keep=libname memname name) end=last;
 if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify have;rename');
 call execute(cats(name,'=',name,'_daily'));
 if last then call execute(';quit;');
run;
AZIQ1
Quartz | Level 8

Hi,

I used this code and it worked successfully. But before this step I want to remove "_" from all my variable names - is there a quick way? I tried using compress with your code but failed. 

Thank you.

Tom
Super User Tom
Super User

The same steps should apply.  Try just running the part that calculates the new variable name from the old name in a data step so you can look at the result and figure what is happening.

 

new_name = compress(name,'_');
AZIQ1
Quartz | Level 8
Thank you, now need a step to keep the ones without '_' as is. My data has a mix of vars with '_' and some without.
Ksharp
Super User
if not findc(name,'_') then  call execute(cats(name,'=',name,'_daily'));
AZIQ1
Quartz | Level 8

Thank you can I apply the same logic or code to change the labels for all my variables - i mean i want to add a term before the label name for all vars:

 

something like this:

PROC CONTENTS DATA=sat1 OUT=tmp_sat2;
RUN;
data _null_;
set WORK.tmp_SAT2(where=(libname='WORK' and memname='SAT1') keep=libname memname label) end=last;
if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify SAT1;LABEL');
call execute(cats(label,'=','SAT_',label));
if last then call execute(';quit;');
run;

 

Your feedback is highly appreciated

Ksharp
Super User
Sure. Your code looks good .
AZIQ1
Quartz | Level 8

Hi,

Its giving me errors - Variable not found!

 

Help

Ksharp
Super User
Can you post the content of table tmp_SAT2 , and in call execute(cats(label,'=','SAT_',label)); you use the same variable name at the both side, That doesn't look right.

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!

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
  • 11 replies
  • 1615 views
  • 5 likes
  • 5 in conversation