DATA Step, Macro, Functions and more


Posts: 0


How do I can get the entire dataset for all the counties instead of just the subset of dataset I'm getting with this code (it is subsetted by &conum which is the macro code for county number)

%macro county_name;
%if &conum=01 %then %let county=MONROE;
%else %if &conum=02 %then %let county=ALPINE;
%mend county_name;

data sch_update;
length co 4;
update kaimport(in=a) sch(in=b);
by schcode;
if a and co=%eval(&conum);
Posts: 0


This looks like poorly constructed code to me.

sch_update is a dataset that is a result of updating kaimport with sch.
I think the use of (in=a) and (in=b) is unnecessary.
Also, I don't like the look of using the macro and an "IF" selection for the subsetting. There are better, clearer, cleaner, simpler ways to do that kind of stuff.

doesn't kaimport have all the data?
Does sch_update get built/renamed into kaimport?

Simple thing:

data outdata;
set indata;
some processing stuff;
Posts: 0


Thanks Chuck!
Posts: 8,740


If this is code that you have inherited and if the code was/is working the way you want and if you need to modify the way the macro code or SAS process is operating now, then I recommend that you do not change the current process, but instead, leave it intact.

Then, I recommend that you start to analyze the existing process by tracing -- all the way back to the beginning of the code-- what all the inputs and outputs to your macro program are. For example, this code snippet shows 3 different files -- all WORK files. There must be some intermediate processing that creates at least 2 of these 3 files -- where do WORK.KAIMPORT and WORK.SCH come from? How are they created and what do they represent. It is possible that one of both of them contains all the counties that you want.

Without understanding how and where WORK.KAIMPORT and WORK.SCH get created, it is impossible to tell you how to proceed. There -are- valid reasons for using an UPDATE statement -- generally, UPDATE is used in a "master/transaction" scenario, where one file contains the "master" dataset and the other file contains "transactions" or "updates" (in old batch processing lingo) which should be applied to the master file based on some common variable. To learn more about the UPDATE statement, consult the documentation. You want the UPDATE statement (NOT the SAS Component Language UPDATE function).

In your code snippet, WORK.KAIMPORT is in the syntax position of the MASTER dataset and WORK.SCH is in the syntax position of the TRANSACTION dataset. Based on this information, then either WORK.KAIMPORT or the dataset from which WORK.KAIMPORT is derived might contain the information that you want/need.

(The only thing that the %EVAL is doing was making sure that &CONUM was treated as a number in the subsetting IF. You should get the same results whether the %EVAL is there or not. I suspect that cocode is a character code and that somebody coded multiplying it by 1 to create the CO variable as a number -- probably because they wanted or needed a numeric version of the country code.)

If you have both WORK.KAIMPORT and WORK.SCH in place (undeleted) after the code runs, you might try this and see whether the resulting file contains what you want:
data what_is_ka;
length co 4;
set work.kaimport;

proc freq data=what_is_ka;
tables schcode co cocode;

proc print data=what_is_ka;

Or, if you only get the (in=a) records, which are coming from WORK.KAIMPORT, you might first try a PROC PRINT on WORK.KAIMPORT to see whether it has all the counties you need. The other thing to do is to trace farther back in the program and try to see where the FIRST place is that &CONUM gets set and where the FIRST place is that WORK.KAIMPORT gets created.

You might also consider contacting Tech Support for help. They can look at ALL of your code and ALL of your data and help you figure out the best way to proceed to code for your new requirements. Learning about SAS macro variables and macro programming can seem like a daunting task on top of learning how SAS operates. But it is do-able. The Macro documentation is very good and there are a number of user group papers that can give you a broad overview of macro processing concepts.

Again, if this is legacy code that you have from someone else, and if it's working as desired, then I advise you not to change -THIS- program until you really understand what it's doing -- master/transaction updates were/are used to mimic mainframe type batch transaction processing -- so I would advise against recoding that program until you really understand what the input files and output files all represent and how they're transformed at the various stages of your program.

Posts: 0


Hi Cynthia:

Thanks for taking a look at this. I appreciate your advice and your explanations. Thanks for explaining % EVAL and for the reference to the UPDATE statement. I also appreciate your suggestion about not altering the code until I have a I understand exactly what's going on. I'm working on that.

Thanks so much!
Ask a Question
Discussion stats
  • 4 replies
  • 2 in conversation