I have searched around, but no luck finding an answer (perhaps I am using the wrong terms).
I have arrays set up to reference full lists of variables, but they are becoming quite bulky. Is it possible to reference only a few characters in the middle of a variable name?
In stata it would look like this:
for each x in cath fall {
use `datafile', clear
collapse (mean) measure_`x'_value ///
pred_measure_`x'_value
[aweight = measure_`x'_count], by (state)
I am not worried about the collapse or aweight portions, but I am trying to find out if it is possible to replicate that reference to `x' and cycle through just the middle of the variable name. In stata, the `x' would be replaced to measure_cath_value and measure_fall_value etc.
Thanks for any help!
Since you are having trouble explaining in word try providing an example. Show a dataset with three or for variable names and explain what you want to do.
If you want to select variable names you need to do that BEFORE the data step is compiled.
The simplest way is to put the list of variable names into a macro variable which you can then use to generate the list of names needed for the ARRAY statement.
For example if you wanted the names of all character variables that have the letter X in their name you could do this:
proc contents data=have noprint out=contents; run;
proc sql noprint;
select nliteral(name) into :namelist separated by ' '
from contents
where upcase(name) like '%X%'
and type=2
;
quit;
You could then use that macro variable to help you write a data step.
data want;
set have;
array x &namelist ;
* some code that references the array X ;
run;
For this, you'd probably just want use a macro in SAS (basically, an .ado file in Stata - not what Stata calls a macro). To do what you're after, try:
%macro loop (varlist=);
%let varlist=%cmpres(&varlist);
%do i=1 %to %sysfunc(countW(&varlist, ' '));
%let x=%scan(&varlist,&i,' ');
** your code here -- just reference x by preceding with ampersand ;
** for example... ;
proc means data=some_dataset;
var measure_&x._value;
output out=avg mean=mean_&x;
run;
%end;
%mend; *loop();
%let cath=var1 var2 var3 var4 var5;
%loop(varlist=&cath);
Thanks for the responses! I am adding an excel file and some code that I tried based on responses:
Not sure the XLSX file helps much. It just seems to be a list of variable names.
That would have been better shared as plain text so it was more easily seen and copied.
And it would help if the result was also provided.
So your macro recreating the same dataset over and over.
data work.example1;
set work.example;
data work.example1;
set work.example;
data work.example1;
set work.example;
So at the end of the macro you will have just the LAST version of EXAMPLE1.
What is it that you are trying to do? Perhaps you just need to move the %DO loop so it is generating a series of statement inside of ONE data step. At least then you will have a chance of changing more than one variable's values.
data work.example1;
set work.example;
%do .....
%end;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.