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

When specifying variable name,I'm looking for how to designate variables in the middle match for the variable name.

 

data _test;
 VAR_A_A=1;
 VAR_B_A=2;
 VAR_C_A=3;

 VAR_A_B=100;
 VAR_B_B=200;
 VAR_C_B=300;
 run;
 
 data _null_;
  set _test;
   Total_by_colon=sum(of VAR:);put "Total is " Total_by_colon;
  run;


In this situation, I wanna sum "VAR_A_A" "VAR_B_A" VAR_C_A", which should be ending with answer 6(=1+2+3).

But we know,If we write
sum(of VAR:);
This ansiwer is 606(=1+2+3+100+200+300).

606.JPG

 

From a different point of view, I want to use a regular expression when specifying vars. But it will not be.

I think there is a need to create a macro?

 

If you have the wisdom, thank you in advance.
Regards,t_ar_taat

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
Yeah. Make a macro variable to hold it.




data _test;
 VAR_A_A=1;
 VAR_B_A=2;
 VAR_C_A=3;

 VAR_A_B=100;
 VAR_B_B=200;
 VAR_C_B=300;
 run;
proc transpose data=_test(obs=0) out=temp;
var _all_;
run;
proc sql;
select _name_ into : list separated by ','
 from temp
  where upcase(_name_) like 'VAR~_%~_A' escape '~';
quit;
data _null_;
  set _test;
   Total_by_colon=sum(&list);put "Total is " Total_by_colon;
  run;


View solution in original post

6 REPLIES 6
Reeza
Super User

You can create a macro or use the VVALUEX logic and obtain the values. 

t_ar_taat
Quartz | Level 8

Hi,Reeza

Thank you for giving me your knoledge very quickly.I'll try it.

Ksharp
Super User
Yeah. Make a macro variable to hold it.




data _test;
 VAR_A_A=1;
 VAR_B_A=2;
 VAR_C_A=3;

 VAR_A_B=100;
 VAR_B_B=200;
 VAR_C_B=300;
 run;
proc transpose data=_test(obs=0) out=temp;
var _all_;
run;
proc sql;
select _name_ into : list separated by ','
 from temp
  where upcase(_name_) like 'VAR~_%~_A' escape '~';
quit;
data _null_;
  set _test;
   Total_by_colon=sum(&list);put "Total is " Total_by_colon;
  run;


t_ar_taat
Quartz | Level 8

HI,Xia

Thank you very much.

I confirmed your codes work well,in short,answer is actually "6".6.jpg

 

 

ballardw
Super User

Some times picking your variable names may be sufficient. Please look at this:

data _test;
 VAR_A_A=1;
 VAR_A_B=2;
 VAR_A_C=3;

 VAR_B_A=100;
 VAR_B_B=200;
 VAR_B_C=300;

 Sum1 = sum(of Var_A:);
 Sum2 = Sum(of Var_B:);
 run;
t_ar_taat
Quartz | Level 8

Hi,ballardw

 

Yes,actually the way you showed is also good.

Thank you for your posting.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 1514 views
  • 0 likes
  • 4 in conversation