BookmarkSubscribeRSS Feed
delgoulding
Calcite | Level 5

I want to create a macro to dynamically catx a percentage with a lower and upper CI.

 

The variables always look like this:

a_percent a_low a_high

b_percent b_low b_high

c_percent c_low c_high

 

 

 Originally I was trying to put the suffix as a prefix so I can create arrays for percents, lows, and highs. Then I would be able to reference the arrays in a catx macro. 

 

data _null_; 
if 0 then set have;


array percent {*} percent:;
array low {*} low:;
array high {*} high:;


call symputx("dim_percent", dim(percent));
call symputx("dim_low",dim(low));
call symputx("dim_high", dim(high));

 

do i = 1 to dim(percent);
call symputx(cats("percent_",i),vname(percent{i}));
end;
do i = 1 to dim(low);
call symputx(cats("low_",i),vname(low{i}));
end;
do i = 1 to dim(high);
call symputx(cats("high_",i),vname(high{i}));

end;

run;

 

And the ultimate goal is to be able to loop through something like this: 

var1=catx("^n(", put(percent(i), percent20.3), put(low(i), percent20.3));
varwant=catx(", ", var1, put(high(i),percent20.3))||")";

4 REPLIES 4
Reeza
Super User
Ok, so what is your question for us?

My recommendation for something like this, is not to roll your own. Instead, reformat your data so you have a data set with:

Value, UCLM, LCML

Then you can use a single formula to create your values. Then if you need a different structure, reformat your data again.
Astounding
PROC Star

Clearly, you have started off on the wrong foot.  Consider this statement:

 

 

array percent {*} percent: ;

 

It appears that the intent is to take all variable names that contain "percent" somewhere in the name.  But that's not what it does.  It actually takes all variable names that begin with "percent" and it appears that you don't have any.

 

I agree with Reeza that you would make the problem a lot simpler by using a different structure for your data.

delgoulding
Calcite | Level 5

Hi! Thank you for your responses! Let me clarify my question. I am not very familiar with arrays and have very large dataset that I am trying to put together the percentage with CIs.

 

So my first step would be renaming my variables to having matching prefixes. However, I am unsure how to more forward after that. I have the code below, but I can only get var1 to output once and the variable I want never outputs. 

 

 

data want; set have;

rename a_percent= percent_a;
rename a_low=low_a;
rename a_high=high_a;
rename b_percent= percent_b;
rename b_low=low_b;
rename b_high= high_b;
run;

 

%let outcomes= a b;

 

data want2; set want;

array percent {*} percent:;
array low {*} low:;
array high {*} high:;
array stat {*} &outcomes;

 

do i= 1 to dim(percent);
do j= 1 to dim(low);

var1=catx("^n(", put(percent(i), percent20.3), put(low(j), comma20.3));

end;
end;

 

do k= 1 to dim(high);
do s= 1 to dim(stat);
stat(s)=catx(", ", var1, put(high(k), comma20.3))||")";
end;
end;


run;

 

Thank you in advance! I really appreciate any feedback/help!

Reeza
Super User
Go back 5 steps. How did you get your data? What does the input data look like? What do you want as output?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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