BookmarkSubscribeRSS Feed
Zen_aly
Calcite | Level 5

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!

4 REPLIES 4
Tom
Super User Tom
Super User

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;
quickbluefish
Barite | Level 11

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);
Zen_aly
Calcite | Level 5

Thanks for the responses! I am adding an excel file and some code that I tried based on responses:

 

%macro loop (varlist=);
  %let varlist=%cmpres(&varlist);
  %do i=1 %to %sysfunc(countW(&varlist, ' '));
    %let x=%scan(&varlist,&i,' ');
     data work.example1;
     set work.example;
if . < Measure_&x._value < 0 then Measure_&x._value=0;
else if Measure_&x._value > 40 then Measure_&x._value=40;
if visit_&x._count < 40 then do;
Measure_&x._value=.;
visit_&x._count=0;
run;
    %end;
%mend;  *loop();
%let CY25=100 abc def 222 ghi jklmno 300 350 399 pqrs;
 
%loop(varlist=&CY25);
 
The above code runs with no error but the changes I asked for did not take place. I just don't seem to get some of the details that I need to successfully create a wide variety of macros.
Tom
Super User Tom
Super User

Not sure the XLSX file helps much.  It just seems to be a list of variable names. 

Tom_0-1742583315692.png

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 764 views
  • 3 likes
  • 3 in conversation