SAS: create a Macro that add suffix to variables in a dataset - sas

I would like to create a macro that add a suffix to variable names in a dataset. below is my code:
%macro add_suffix(library=,dataset=,suffix=);
proc sql noprint;
select cat(name, ' = ', cats('&suffix.',name )) into :rename_list separated by ' ' from
dictionary.columns where libname = '&library.' and memname= '&dataset.';
quit;
proc datasets library=&library nolist nodetails;
modify &dataset;
rename &rename_list;
run;
quit;
%mend;
%add_suffix(library=OUTPUT,dataset=CA_SPREADS,suffix=CA);
It gives error messages:
NOTE: No rows were selected.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
WARNING: Apparent symbolic reference RENAME_LIST not resolved.
NOTE: Line generated by the invoked macro "ADD_SUFFIX".
2 rename &rename_list; run;
-
22
76
NOTE: Enter RUN; to continue or QUIT; to end the procedure.
ERROR 22-322: Expecting a name.
ERROR 76-322: Syntax error, statement will be ignored.
If I put the library and dataset names in quotation mark, it works for the first block i.e. add values to string rename_list but not for the proc dataset step

Macro triggers like % and & are not honored inside of single quotes. That is why you are not getting any hits on your SQL query. There is no library name that has an & as the first character.
The reason it looked like it was sort of working is that when you use this in your SQL statement
catx('=',name,cats('&prefix.',name))
then you end up with a string like
age=&prefix.age
And that will actually work because the reference to the macro variable PREFIX will resolve when you run the RENAME statement.
You should just use double quotes instead.
%macro change_names(library=,dataset=,prefix=,suffix=);
%local rename_list;
proc sql noprint;
select catx('=',name,cats("&prefix",name,"&suffix"))
into :rename_list separated by ' '
from dictionary.columns
where libname = %upcase("&library")
and memname = %upcase("&dataset")
;
quit;
%if (&sqlobs) %then %do;
proc datasets library=&library nolist nodetails;
modify &dataset;
rename &rename_list;
run;
quit;
%end;
%else %put WARNING: Did not find any variables for &library..&dataset..;
%mend change_names;
%change_names(library=OUTPUT,dataset=CA_SPREADS,prefix=CA);

Your macro variables are not being resolved because you're wrapping them in single quotes ' rather than double quotes ".
You should uppercase the libname and memname parameters of your macro as these are always in uppercase in dictionary.columns.

Tested and works. Longer but maybe more beginner friendly approach. Input the dataset name and what suffix you want to add.
example: %add_suffix(orders, _old); /* will add _old suffix to all variables.*/
%macro Add_Suffix(Dataset, suffix);
proc contents noprint
data=work.&dataset out=sjm_tmp(keep=NAME);
run;
data sjm_tmp2;
set sjm_tmp;
foobar=cats(name, '=',NAME,'&suffix.');
run;
proc sql noprint;
select foobar into :sjm_list separated by ' ' from sjm_tmp2;
quit;
proc datasets library = work nolist;
modify &dataset;
rename &sjm_list;
quit;
proc datasets library=work noprint;
delete sjm_tmp sjm_tmp2 ;
run;
%mend Add_Suffix;

Related

macro for proc sql select into, good for i=1 but not i=2

I was trying to use proc sql select into to generate macro variables, and I would like to do it for all variables in the code dataset, so I embedded it within a macro. The proc sql runs ok for both cd_1 and cd_2 as there are valid print out. However, only cd_1 has the value I wanted, and cd_2 was not resolved. I have pasted the full code below.
data code;
infile datalines delimiter=',';
input MUSCLE $ STIMULANTSFL $ ;
datalines;
baclofen,amphetamine
carisoprodol,dexmethylphenidate
;
run;
*Selecting all the variables names;
proc sql ;
select name into : vars1 - :vars2 from dictionary.columns
where LIBNAME = 'WORK' and MEMNAME = 'CODE';
quit;
*for all names select the codes;
%macro getcode;
%do i=1 %to 2;
PROC SQL ;
select (trim(&&vars&i.)) into : cd_&i. separated by '|' from code where (trim(&&vars&i.)) ne '';
quit;
%end;
%mend;
%getcode;%put &cd_1;%put &cd_2;
Macro variable scope issue - local versus global.
I suspect your first one only works because you tested it. Move the %PUT to inside the macro to have it resolve. Your macro variables do not exist outside your macro unless you explicitly set them to exist.
Add %GLOBAL to create global macro variables.
data code;
infile datalines delimiter=',';
input MUSCLE $ STIMULANTSFL $;
datalines;
baclofen,amphetamine
carisoprodol,dexmethylphenidate
;
run;
*Selecting all the variables names;
proc sql;
select name into : vars1 - from dictionary.columns where LIBNAME='WORK' and
MEMNAME='CODE';
quit;
option mprint;
*for all names select the codes;
%macro getcode;
%do i=1 %to 2;
%global cd_&i;
PROC SQL;
select (trim(&&vars&i.)) into : cd_&i. separated by '|' from code
where (trim(&&vars&i.)) ne '';
quit;
%put cd_&i.;
%end;
%mend;
%getcode;
%put &cd_1.;
%put &cd_2.;

SAS - Exporting same dataset to multiple excel files

I would like to export the dataset to multiple excel files based on a certain variable:
proc sql;
create table try as
select distinct make from sashelp.cars;
quit;
proc sql;
create table try2 as
select count(make) as aaa from sashelp.cars;
quit;
data _null_;
set try;
by make;
call symputx ('make',compress(make,' .'),'g');
run;
data _null_;
set try2;
call symputx('n',aaa);
run;
%macro a;
%do i=1 %to &n;
%let var= %scan(&make,&i,"#");
proc export data=testing (where=(make="&make."))
outfile="C:\Users\&make..xlsx"
dbms=xlsx replace;
sheet="&make." ;
run;
%end;
%mend ;
%a;
My goal is to get all the 38 excel files with the maker name as the filename.
However, all I am able to get here is the last maker name's file.
Would you please point out where I am missing out here? Many thanks!!
Your first error is that you count the number of cars that have a make, while you should count the distinct makes of cars. Now let me also take the opportunity to explain you the into clause of sql, so you don't need that data step anymore
proc sql;
select count(distinct make)
into :make_count
from sashelp.cars;
quit;
You remove blanks and point from your make names, but you better remove all non-alphabetic characters at once, with compress(make, '', 'ka'), in which the options k stands for keep and a stands for alphabetic.
Your main error is that you think you append all make names in the macro variable make, but you actually overwrite make time and again: first you write "Cadillac" to it, then "Chevrolet" and by the time you ever use it, it became "Volvo".
I could explain you how to correct your datastep, but instead, I will learn you an option of that into statement:
proc sql;
select distinct compress(make, '', 'ka')`
into :make_list separated by ' '
from sashelp.cars;
quit;
The rest is easy.
%macro export_by_make;
%do make_nr=1 %to &make_count;
%let make= %scan(&make_list, &make_nr);
proc export data=sashelp.cars (where=(compress(make, '', 'ka')`="&make."))
outfile="C:\Users\&make..xlsx"
dbms=xlsx replace;
sheet="&make." ;
run;
%end;
%mend;
%export_by_make;
Note that you don't need to specify a separator for the %scan function, as we separated by blanks, but anyway, if you do, as you use the macro version of scan, you don't need the quotes around it.

Proc Sort Using a Macro

I created 40 plus tables (using a marco, which I just learned how to do) that I would like to apply the Proc Sort statement to. I want each table sorted by the same variable 'Account_Description' (each table contains this variable).
The table names are June_53410_v1, June_53420_v1, June_53430_v1, etc. Can I employ a macro, and if so, how can I, to mitigate having to write a proc sort statement for each table?
Thanks!
I found this sample code online but I'm not really sure how it works
%Macro sorter(dsn, var);
proc sort data=&dsn.;
by &var.;
run;
%mend;
%sorter(sample_dataset, age);
Macro that will be used (proc sort write to work):
%Macro sorter(lib,dsn, var);
proc sort data=&lib..&dsn. out=&dsn.;
by &var.;
run;
%mend;
Get dictionary of tables that contains in name some chars (its maby “June_” instead “AIR”) :
data sashelp_tables;
set sashelp.vtable;
where LIBNAME="SASHELP" and MEMNAME contains "AIR"
;
run;
Write code to string , and execute it for all tables:
data _NULL_;
length code $ 200;
set sashelp_tables;
code=cat('%sorter(',LIBNAME,',',MEMNAME,',AIR);');
call execute(code);
run;
I appreciate everyone's input-I think I found an answer though using this code:
%macro st (ds);
proc sort data = &ds;
by Account_Description;
run;
%mend;
%st(June_53410_v1);
%st(June_53420_v1);
You can use this solution, where lib is libname, mask_table is mask to table(June_ in your task) and var is variable to sort tables:
%macro sorter(lib,mask_table, var); %macro d;%mend d;
%let table_list = 0;
proc sql noprint;
select strip(libname) || '.' || strip(memname),count(memname)
into: table_list separated by ' '
from dictionary.tables
where libname = UPCASE("&lib.") and memname LIKE UPCASE("&mask_table.")||"%";
quit;
%do i=1 %to %sysfunc(countw(&table_list,%str( )));
%let name&i = %scan(&table_list, &i, %str( ));
proc sort data=&&name&i.;
by &var.;
run;
%end;
%mend sorter;
%sorter(WORK,June,Account_Description);

SAS : keep if exists

I have several databases, one per geographical variables, that I want to append in the end. I am doing some data steps on them. As I have large databases, I select only the variables I need when I first call each table. But on tables in which one variable always equals 0, the variable is not in the table.
So when I select my (keep=var) in a for loop, it works fine if the variable exists, but it produces an error in the other case, so that these tables are ignored.
%do i=1 to 10 ;
data temp;
set area_i(keep= var1 var2);
run;
proc append base=want data=temp force;
run;
%end;
Is there a simple way to tackle that ?
In fact I have found a solution : the DKRICOND (or DKROCOND) options specify the level of error detection to report when a variable is missing from respectively an input (or output) data set during the processing of a DROP=, KEEP=, or RENAME= data set option.
The options are DKRICOND=ERROR | WARN | WARNING | NOWARN | NOWARNING, so you just wave to set
dkricond=warn
/*your program, in my case :*/
%do i=1 to 10 ;
data temp;
set area_i(keep= var1 var2);
run;
proc append base=want data=temp force;
run;
%end;
dkricond=error /* the standard value, probably better to set it back after/ */
How about just adding it to the table if it doesn't already exist?
/*look at dictionary.columns to see if the column already exists*/
proc sql;
select name into :flag separated by ' ' from dictionary.columns where libname = 'WORK' and memname = 'AREA_I' and name = 'VAR1';
run;
/*if it doesn't, then created it as empty*/
%if &flag. ne VAR1 %then %do;
data area_i;
set area_i;
call missing(var1);
run;
%end;

How do you read multiple specific datasets and append to one big dataset?

How do you read multiple specific datasets and append to one big dataset?
For example I within a library I have 100s of datasets but I only want to append the datasets that have _du1, _du2
The format and column names are the same
My stab of it doesnt work:
PROC SQL NOPRINT;
SELECT memname INTO :tab1-:tab103 FROM sashelp.vtable
where memname like '_DU%';
SELECT count(*) INTO :obs FROM sashelp.vtable
where memname like '_DU%';
QUIT;
%macro rubber;
%do i=1 %to i=&obs;
proc append base=tot_comb data=&&tab&i force;
run;
%end;
%mend;
%rubber;
PROC APPEND may not actually be faster in this case, or at least not faster by enough to justify doing it, than just writing a datastep.
data tot_comb;
set work._DU:; *or your libname;
run;
This will work if you are on SAS 9.2 or later. If you're on 9.1 or earlier, you'll need to do one proc sql step, like
proc sql;
select memname into :namelist separated by ' '
from dictionary.columns
where libname='WORK' /* or your libname */
and memname eqt '_DU';
quit;
*eqt is like starts with;
data tot_comb;
set &namelist;
run;
That only requires one pass to write, and I'm not sure it will be much slower than so many calls to PROC APPEND.
Here is some code that will get you all the data set names from a given library with some characteristics (starts with _DU). You could use the final macro in a variety of ways to append data sets.
Data _DU1;
var="One";
Run;
Data _DU2;
var="Two";
Run;
PROC SQL;
create table main as
SELECT *
FROM DICTIONARY.COLUMNS
WHERE UPCASE(LIBNAME)="WORK" AND
UPCASE(MEMNAME) like '_DU%';
Select memname
into :dsn separated by ' '
from main;
QUIT;
%Put &dsn;
EDIT (according to your comment)
I added some UPCASE statements and used your count macro var for the number of tab macros
Narrowing your where statement should make your code more efficient
Try this (some of the code is untested):
PROC SQL NOPRINT;
SELECT count(*)
INTO :obs
FROM sashelp.vtable
where UPCASE(LIBNAME)="<YOUR LIB IN UPCASE>" AND
upcase(memname) like '_DU%';
%Let obs=&obs;
SELECT memname
INTO :tab1-:tab&obs
FROM sashelp.vtable
where UPCASE(LIBNAME)="<YOUR LIB IN UPCASE>" AND
upcase(memname) like '_DU%';
QUIT;
%macro rubber;
%do i=1 %to &obs;
proc append base=tot_comb data=&&tab&i force; run;
%end;
%mend;
%rubber;