Hello,
I have tested the code below and it works well. But I found that if we run the script again the anonymized dataset cannot be generated by append.
How can we do that.
Here's the orginal code
data dest1.&filename dest2.&filename._Anonym; /* Clean dataset and anonymized dataset */
merge source4.&filename. (in=inbase) /* premium dataset in the subfolder related to Data_Retention*/
src1.&dsname. (in=inlist where=(filename = "&filename.")) ; /* The polices of the Master List and their location into the various premium dataset */
by ORGNL_SAS_DS_ROW_NBR;
if inlist and inbase then output dest2.&filename._Anonym;
else if inbase then output dest1.&filename.;
run;
So, I need to replace the if inlist and inbase then do; proc append base=dest2.&filename._Anonym data=??????;run;end;
The only way to conditionally call PROC APPEND (or any other PROC) is with an %IF statement (in a macro or in open code). You can't do that in a DATA step, that is invalid syntax. Something like this (pseudo code so you get the idea):
data octopus;
...
if condition1 then call symputx('macrovar',0);
else if condition2 then call symputx('macrovar',1);
run;
%if ¯ovar %then %do;
proc append ... ;
run;
%end
I do not understand what your issue is.
Your current data step is generating two output datasets.
What is it that you want to use PROC APPEND to do?
I have tested my code and I have found that for the else condition, an output statement is fine. But for the if condition, I do not need to replace the dataset but to append the data generated.
So my question is how do we do that. How do we convert the condtion
if inlist and inbase then output dest2.&filename._Anonym;
if inlist and inbase then
do;
proc append base=dest2.&filename._Anonym; data= what do we put here;
run;
@alepage wrote:
I have tested my code and I have found that for the else condition, an output statement is fine. But for the if condition, I do not need to replace the dataset but to append the data generated.
So my question is how do we do that. How do we convert the condtionif inlist and inbase then output dest2.&filename._Anonym;
if inlist and inbase then do; proc append base=dest2.&filename._Anonym; data= what do we put here; run;
You cannot put a PROC inside a DATA step. I already provided an earlier post in this thread explaining what to do instead.
This whole thread is the XY Problem. You have a "solution" that requires you to put PROC APPEND into a DATA step, but you are having trouble doing it. You haven't told us the problem that this allegedly solves, and it would be extremely helpful if you backed up and explain the entire problem you are trying to solve, rather than focusing on your solution of putting a PROC into a DATA step. Please note that backing up and explain the entire problem, rather than focusing on your desired solution, is a far superior approach that can produce much better solutions, involving less code and less confusion.
So if we're going to move forward, we first have to move backward and understand what the problem is that you are trying to solve with a PROC in a DATA step.
Two general solutions.
Leave your data step essentially the same, but write the "APPEND" records to a temporary dataset. Then use a second step running PROC APPEND to add that temporary dataset to the permanent dataset.
Write the matching records to a dataset, let's call it BOTH. Write the unmatched BASE records to another temporary dataset, let's call it TEMP_BASE. Then append that temporary dataset to some existing (or not existing dataset since PROC APPEND does not care) permanent dataset, let's call that PERMANENT_BASE.
data BOTH TEMP_BASE ;
merge BASE (in=inbase)
LIST (in=inlist where=(filename = "MYFILENAME"))
;
by BYVAR ;
if inlist and inbase then output BOTH;
else if inbase then output TEMP_BASE;
else /* Ignore the LIST_ONLY records */ ;
run;
proc append data=TEMP_BASE base=PERMANENT_BASE;
run;
NOTE: Once you know what code you want to generate you can start replacing some of the code with macro variable references.
The other way is to use a MODIFY statement which will allow you to use the APPEND statement to append observations to a dataset in a single data step. You can also use an OUTPUT statement to write those other observations to some other output dataset.
It is hard to tell from your dataset names and your descriptions so far exactly what you are trying to do. But perhaps it is something like this:
You have two input datasets named BASE and LIST and you want to combine them to generate observations that are either appended to PERMNENT or written to a brand NEW dataset.
data permanent new ;
if 0 then modify permanent;
merge base(in=inbase) list(in=inlist);
by keyvar;
if inbase and inlist then output new;
else if inbase then append;
run;
But somehow I doubt it. More likely you will want to use the KEY= option on the MODIFY statement to get it to work.
Providing a simple example of the inputs with just enough observations to demonstrate the different possible situation will make it much easier to design and test different solutions.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.