Hi all,
I have a dataset which includes two timepoints:
TP_1 TP_2
t1 t4
t2 t3
t3 t2
t1 t1
t2 t4
t3 t3
I have a complex step which involves comparing two variables in the data file to see whether they are equivalent. This uses macro processing. The problem is, I am having a lot of trouble letting the macro know which timepoints are in effect for the record. This is important because the set variables that are being compared are determined based on the timepoint.
How do I macrotize the timepoint(s) so that I can use all my macro commands? There will be one set of timepoints per record which will be used throughout all var comparisons.
Thanks,
Hi, it's not clear to me what you're exactly trying to compare. You mean something like this?
data have;
input TP_1 $ TP_2 $ ;
datalines;
t1 t4
t2 t3
t3 t2
t1 t1
t2 t4
t3 t3
;
run;
%macro test(inds=,outds=,timepoint1=,timepoint2=);
%if &timepoint1. ne &timepoint2. and &timepoint1. ne and &timepoint2. ne %then %do;
data &outds.;
set &inds.;
if &timepoint1. eq &timepoint2. then put "NOTE: " &timepoint1.= "eq" &timepoint2.= "at" _N_=;
run;
%end;
%mend test;
%test(inds=have,outds=want,timepoint1=TP_1,timepoint2=TP_2);
- Cheers -
Sorry, I should have been more clear.
I have 3 sets/lists of variables based on tp_1 and 4 sets/lists based on tp_2:
vlist1_tp_1_t1 = v1_t1 v2_t1...v100_t1;
vlist2_tp_1_t2 = v1_t2 v2_t2...v100_t2;
etc. for every value of tp_1 and tp_2.
Basically I have a macro set up which cycles through the var lists and does variable comparisons. The problem is that, since the timepoints are not macrotized, I have no way to feed them into the macro so I can direct SAS to the appropriate var list for the record.
So for example, if tp_1 = t1 and tp_2 = t1, I need a macrotized way of pointing SAS to vlist1_tp_1_t1 and vlist_tp_2_t1 for that record.
Macros are for generating CODE. It does not sound like code generating is what you are after.
Just write normal SAS code to handle your situation. If you want to repeat a series of operations over multiple variables use an array.
So perhaps you want to do something like:
data want;
set have;
array v1 v1_t1 v2_t1 ... v100_t1;
array v2 v1_t2 v2_t2 ... v100_t2;
if var='T1' then do ;
* code using V1 array ;
end;
else do;
* code using V2 array;
end;
run;
If you really have 200 visit variables you might find that transposing the data will make whatever you are trying to do easier. Then it becomes a question of finding the right observation based on the value rather than the right variable. Which is something a simple join can handle.
Still clear as mud.
Can you provide some actual values, they don't have to senstive just data that behaves the same, and what the result for the examples should be. Don't need to provide lots of values or variables, maybe 3 or 4 variables and 5 records and what comparison code you are currently using.
Without more details it appears that there may be a data structure problem as the bit of the description I think I understand sounds like you have moved information from data fields to variable names.
FWIW, any code result will probably be easier to write if instead of
vlist1_tp_1_t1 = v1_t1 v2_t1...v100_t1;
The variables were named
vlist1_tp_1_t1 = t1_v1 t1_v2 ...t1_v100;
There are shorthand lists, such as for array definitions and a number of functions that accept multiple variables where you could t1_v: to reference all variables whose names start with t1_v or a sequential list like t1_v1 - t1_v10 to use those 10 variables. Otherwise you have to write them out all the time.
In general placing lots of variable values into macro variables results in hard to understand and frequently fragile code.
Still not clear to me why you need a macro for which operations and results exactly.
I think posting portion of your job could be a better alternative rather than trying to describe it.
Based on your comment
So for example, if tp_1 = t1 and tp_2 = t1, I need a macrotized way of pointing SAS to vlist1_tp_1_t1 and vlist_tp_2_t1 for that record.
You mean like this?
%let tp_1=t1;
%let tp_2=t4;
%let SASpoint1=vlist1_tp_1_&tp_1.;
%let SASpoint2=vlist2_tp_2_&tp_2.;
%put &=SASpoint1;
%put &=SASpoint2;
*or this?;
data _NULL_;
length tp_1 tp_2 $10;
tp_1='t1';
tp_2='t1';
call symputx('SASpoint3',cats('vlist1_tp_1_',tp_1));
call symputx('SASpoint4',cats('vlist1_tp_1_',tp_2));
run;
%put &=SASpoint3;
%put &=SASpoint4;
%symdel SASpoint1 SASpoint2 SASpoint3 SASpoint4;
- Cheers -
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.