Hi everyone! So my question is multifold, but just some background: I'm trying to import a bunch of excel data for a decade of all 50 states, rearrange them, and ultimately merge them together
All excel sheets are in same format. Each time I import a sheet to run the code, I want to:
1. The state name is in the same Excel box for every sheet. I would like to retrieve the data in this box, hold it (probably as a variable "state"), then rewrite it as "State1" -- something generic so I can apply the code generically
2. Could I then take the data stored as "state" within the code, to replace into something else. For instance at the end of the code I have SAS spit out a file which name includes the state's name, could I use this stored variable to label the file instead of manually changing the title every run?
3. Might be asking for too much, but is there a way to code so that my code of importing, rearranging data, etc., so that SAS just takes one file after another (+1 somewhere?) so I don't have to manually adjust the sheet name in the import section?
So that is an easier thing to understand and create code for. We can use any dataset as the example to demonstrate what to do. Let's use SASHELP.CLASS. So just query the metadata of the dataset and find the name of the first variable. You can use DICTIONARY.COLUMNS (or the view SASHELP.VCOLUMN) or PROC CONTENTS output. The variable VARNUM will let you know which is the first variable.
data have;
set sashelp.class;
run;
proc sql noprint;
select nliteral(name) into :oldname trimmed
from dictionary.columns
where libname='WORK' and memname='HAVE' and varnum=1
;
quit;
data want;
set have (rename=(&oldname=STATE));
run;
proc print;
run;
UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...
Is the excel sheet organized as a dataset or not?
Does the first row contain column headers that can by used as variable names?
Does the actual data start in the second row?
Do all of the cells in any given column contain the same type of data?
Or is it just a seemingly random scattering data ?
I guess I should specify that I don't need to hold the name of the state anymore.
Each excel when I import the dataset is always the same. I just want to be able to rename the first column, which is labeled by state name as the variable, as "State" regardless of what its name is when imported. I want to figure out how to rename variables by column position (came across arrays--didn't work well ultimately, and a macro--but unsure if I used it correctly)
This way I wouldn't have to edit the code manually: e.g.
rename Alabama = State
And change Alabama --> another state name every time.
So that is an easier thing to understand and create code for. We can use any dataset as the example to demonstrate what to do. Let's use SASHELP.CLASS. So just query the metadata of the dataset and find the name of the first variable. You can use DICTIONARY.COLUMNS (or the view SASHELP.VCOLUMN) or PROC CONTENTS output. The variable VARNUM will let you know which is the first variable.
data have;
set sashelp.class;
run;
proc sql noprint;
select nliteral(name) into :oldname trimmed
from dictionary.columns
where libname='WORK' and memname='HAVE' and varnum=1
;
quit;
data want;
set have (rename=(&oldname=STATE));
run;
proc print;
run;
I usually just search
https://www.google.com/search?q=%40sas.com+dictionary+views
A couple of useful things I didn't notice when browsing those links:
Sounds like you are not setting the macro variable.
Would need to see the actual code you used, but if you are trying to use CALL EXECUTE to call a macro once for each observation in a dataset with a list of datasets then make sure to use %NRSTR() around the macro call to prevent timing issue.
So instead of doing something like:
data _null_;
set filelist;
call execute('%mymacro('||dataset||')');
run;
Add %NRSTR() like this:
data _null_;
set filelist;
call execute('%nrstr(%mymacro)('||dataset||')');
run;
Plus your SAS log will much easier to read:
+ %mymacro(dataset1) + %mymacro(dataset2) ...
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!
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.