SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
nsns
Quartz | Level 8

Hello,

I have a program with a %let pop=ITT (to choose ITT analysis population) in the beginning.  Within the program I have code that has the condition if &pop.="Y" .  How can I code this so that I can run the entire program for ITT and then for PP population?

Thanks in advance for any ideas.

21 REPLIES 21
PaigeMiller
Diamond | Level 26

Without seeing your program, its nearly impossible to advise.

 

Is ITT the name of a variable, or is it the value of a variable?

 

--
Paige Miller
nsns
Quartz | Level 8
Thank you for your suggestion.

By groups wouldn’t work because of the structure of the datasets. There are variables to identify which observations (ie subjects) belong to which populations. Ie ITT=Y/N, PP=Y/N.
The code is basically

%let pop= ITT
data a;
Set b;
If &pop.=“Y”;

Run;

This repeats a few times in the program bringing in and merging different datasets where the condition &pop.=“Y” is used. There are also some macros within the code. With a summary table at the end.

I Want to produce the same summary table
For the different analysis populations.

I hope this clarifies the issue a bit
PaigeMiller
Diamond | Level 26

I feel uneasy recommending code after seeing an obviously contrived program. However, this is the outline of what you would do.

 

 

%macro do_this(pop);
     data a;
          set b(where=(&pop="Y"));
          ...
     run;
     /* the rest of your code goes here */
%mend;

%do_this(ITT)
%do_this(PP)

 

--
Paige Miller
nsns
Quartz | Level 8

Thanks Paige,

I understand the hesitation.  The program is pretty convoluted with macros embedded inside.  I actually figured out how to get the macro for the populations to work - I added two ampersands i.e.  if &&pop.="Y";  to each place within the program. This seems to work well now.  I am not completely sure why or how this works - but will figure that out. Thanks for your input.

PaigeMiller
Diamond | Level 26

Double-ampersands are not necesssary here, unless there are things going on in your code that you haven't mentioned.

--
Paige Miller
dxiao2017
Pyrite | Level 9

Hi @PaigeMiller I agree with you that double ampersand is not needed here, please see details in my other threads, one ampersand can resolve the macro. However, I am curious about how @nsns use double ampersand to resolve the macro (which obviously resolved twice) and what does the macro look like 😀

PaigeMiller
Diamond | Level 26

I would be happy to discuss these macro questions in their own thread. I will not discuss them in this thread, as they have deviated far from the topic of "Running a program for different populations"

--
Paige Miller
dxiao2017
Pyrite | Level 9

It's okay, @PaigeMiller , you are welcome!

PaigeMiller
Diamond | Level 26

I don't know what is okay, that isn't clear. However, it is not okay, in my opinion, to ask questions about how to use macro variables in this thread, they belong in their own thread, and people (including me) will be happy to help.

--
Paige Miller
dxiao2017
Pyrite | Level 9

Hi Paige, thanks very much for your comments and suggestions! I will pay attention later on!

dxiao2017
Pyrite | Level 9

Hi, so you solved the problem yourself through adding one more ampersand & before the macro &pop.? And then it is resolved to a value like 'ITT' and 'PP', isn't it 😀 , so this macro need to be resolved twice (i.e., double ampersand && should be applied) into the correct value, and the macro you are using is a big complex nested macro which I cannot imagine what does it look like 😀

dxiao2017
Pyrite | Level 9

Hi nsns, thank you for telling me what 'PP' stands for (I don't know what is per protocol analysis population though😀 ), I create a dataset like this and a macro according to what you described, hope this explain part of your problem. Please let me know if it does not solve your question😀.

/*create dataset*/
data weight;
   input subjid weight itt $ pp $;
   datalines;
101 50 y y 
102 55 y y
103 58 y y
104 60 n y
105 66 n y
106 67 y n
107 68 y n
108 70 y n
109 71 y y
110 72 y y
111 80 n y
112 49 y n
112 48 y n
114 49 y y
115 45 y y
;
run;
proc print data=weight;
run;
/*create %let macro to print
dataset for different population*/
%let flag=y;
data weight_itt;
   set weight;
   if itt="&flag";
run;
proc print data=weight_itt;
run;
data weight_pp;
   set weight;
   if pp="&flag";
run;
proc print data=weight_pp;
run;
/*another way to create this macro*/
%macro popset(table,var);
%local table var;
data &table._&var;
   set &table;
   if &var='y';
run;
%mend popset;
%popset(weight,itt);
%popset(weight,pp);

dxiao2017_0-1744888649927.png

dxiao2017_1-1744888715616.png

dxiao2017_3-1744888772717.png

dxiao2017_4-1744888870598.png

dxiao2017_5-1744888942134.png

 

dxiao2017
Pyrite | Level 9

 @nsns I have another question for you: why there is a period (or dot) behind your macro variable &pop (,please see the picture, I marked it out with a red circle)?

Untitled.png

@PaigeMiller , I am not sure whether the %local statement in my second macro write correctly, could you help confirm it? Thanks!

PaigeMiller
Diamond | Level 26

@dxiao2017 wrote:

 @nsns I have another question for you: why there is a period (or dot) behind your macro variable &pop (,please see the picture, I marked it out with a red circle)?

Untitled.png

@PaigeMiller , I am not sure whether the %local statement in my second macro write correctly, could you help confirm it? Thanks!


Please start a new thread with these questions, as they are not really on topic in this thread.

--
Paige Miller

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 21 replies
  • 966 views
  • 8 likes
  • 3 in conversation