12-24-2013 04:11 AM
I have an interview question to get an answer for. The only condition is we are not to use merging for the following and to do it with the help of macros only.
Question: There are two datasets. One is having Subject and visit, other is having visit only.
input subject visit @@;
001 1 001 2 001 3 001 8 002 1 002 8
input visit @@;
1 2 3 4 5 6 7 8
I want another dataset which will compare the visit from first dataset to the second dataset and will give the missing visits in new variable, one observation for each missing visit for each subject. Following is the example of final output required:
subject visit missingvisit
Reminder - not to use the merging
12-24-2013 05:44 AM
Since you have mentioned using Macros only, I'd think of using CALL SYMPUT to place your variable values into a macro variable and call the same(key variable) using SYMGET function. I'd suggest do it in two steps for easy understanding. The syntax is pretty simple as I am sure you would have noticed in support.sas.com.
Hope that helps.
12-24-2013 06:56 AM
Me and my friend (who was asked this question) also thought that this is very simple ;-)
We were on the same track which you suggests i.e. call symput and symget. But can u please give the exact code, how it will take place....
12-24-2013 06:20 AM
I don't know what macro will bring to this party. I like the features of PROC SUMMARY for this. If you have other variables in T as you surely would you can use IDGROUP to pass then through.
12-24-2013 07:02 AM
The method you provided, as i understood created a flag and after this it is simple. But the first thing to it is to use the macro. I tried to use call symput to put each value in a macro variable then i got stuck on how to compare it like vlookup.
I appreciate your effort on this. But we need to do this by macro only, this time.
12-24-2013 07:08 AM
The first rule of macro programming in SAS; is to learn when you don't need it or perhaps more importantly when you don't want to use it.
12-24-2013 11:14 AM
In a similar trend to DN and Tom,
Had I been asked this question in an interview with my current knowledge of SAS, my answer would be straight forward:
I will assume this is a trick question. Whether this can be achieved or not through macros is irrelevant, there are significantly better and more efficient tools for this task than macros and in particular, any macro strict solution for this task should be avoided.
As for a bad answer to a trick question for the sole sake of showing skills with SAS macros,
select distinct subject
call symput(cats('visit', '_', subject, '_', visit), '0'); /* the actual value put in the macro var is irrelevant we only need it to exist. */
%do i=1 %to &nbyvar;
%do j=1 %to &nvisit;
%if %symexist(visit_&i._&j.) %then %do;
%end; %else %do;
Don't get me wrong, it's rarely the best choice to talk against a question but if the rationale as to why macros shouldn't be used to solve this is accompanied by something like "but I assume this is merely for the sake of seeing if I am comfortable with handling macro variable and logic", then here's an example solution that uses right about as much macros as can be. It is probably wiser to demonstrate to the interviewer that not only do you know macro wells but also know when not to macro at the same time~
12-24-2013 10:42 AM
"Macro only" is almost never the answer when he problem involves manipulation of data. For that you need to use data steps and procedures.
This is certainly one of those cases and you should have told them such.