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

Hi,

 

I have the following macro:

 

%macro varz(variable);
data qc2;
set qc;
keep gvkey fyear &variable;
rename &variable=&variable.._qc;
run;
%mend;

%varz(sales);

So here I want to create a data table qc2 which will include the variables gvkey, fyear and sales from the original table qc, and then rename sales to sales_qc, but it is this renaming part which causes errors (if it is omitted then the macro works).

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
error_prone
Barite | Level 11

Don't use a datastep to rename a variable, use proc datasets instead, there is no need to do a full iteration, when you just want to change metadata. And: why do you want a macro? 

 

To solve the error in your code, reduce the number of dots between &variable and _qc to one.

 

 

View solution in original post

4 REPLIES 4
error_prone
Barite | Level 11

Don't use a datastep to rename a variable, use proc datasets instead, there is no need to do a full iteration, when you just want to change metadata. And: why do you want a macro? 

 

To solve the error in your code, reduce the number of dots between &variable and _qc to one.

 

 

ilikesas
Barite | Level 11

I have 2 datasets: qc and cc, and there are variables with identical names in both datasets. I just wanted a quick way to compare the variables from the datasets. Here is the full code:

 

%macro varz(variable);
data qc2;
set qc;
keep gvkey fyear &variable;
rename &variable=&variable._qc;
run;

data cc2;
set cc;
keep gvkey fyear &variable;
rename &variable=&variable._cc;
run;

data want;
merge qc2 cc2;
by gvkey fyear;
run;

data want;
set want;
diff = &variable._qc - &variable._cc;
run;

proc means data=want;
run;

%mend;

%varz(sale);
Patrick
Opal | Level 21

@ilikesas

The following might give you the idea how you could go about this.

data work.demo;
  set sashelp.class;
run;

proc sql noprint;
  select
    cats(name,'= demo_',name) 
      into :rename_list separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='DEMO' and upcase(name) not in ('NAME','HEIGHT')
  ;
quit;

proc datasets lib=work nolist;
  modify demo;
    rename &rename_list;
  run;
quit;

proc contents data=demo;
run;quit;

Alternatively consider using Proc Compare.

error_prone
Barite | Level 11
Automatically renaming variables will work only if you are 100% sure, that each variables name with the appended postfix is shorter than 32 chars.

Depending on what you want to do with the result of the comparison, using proc compare can be the easier solution.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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