SAS Programming

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

I have a question about proc append. If I have such a table as below, which code I run every week and let's assume that next week I will not have any records, e.g. in 2021-12. Will it use proc append add data to me anyway and put 0 in the 2021-12 column. If not, what to use to avoid the situation that he will not give me another row of results because he will not find values in the previous month.

Gieorgie_1-1635412767812.png

PROC SQL;   
   create table POLICY_VINTAGE_WEEKLY as
   select distinct 
   POLICY_VINTAGE
  ,count(NRB) as NUMBER_POLICY
  ,today() as Run_Date format=weeku.
   from PolisyEnd
   where POLIS ="A"
   group by 
   POLICY_VINTAGE
;
Quit;
proc append base=_work.policy_vintage_weekly data=policy_vintage_weekly;
run;

 

5 REPLIES 5
Kurt_Bremser
Super User

The DISTINCT keyword is unnecessary, as you only have the GROUP BY variable, a summary result, and a fixed value in the SELECT. The only possible effect is bad performance.

 

The dataset created in the SQL does in no way match what's in the picture, so your question about the missing column does not make sense.

Gieorgie
Quartz | Level 8
Thank you for your answer Kurt, this is a hypothetical assumption. I mean, when tomorrow I will run a same code and in the Policy_Vintage column with the date 2021-11 there will no longer be records, it will add POLICY_VINTAGE_WEEKLY a new position with the new Run_Date date. If so, whether in position 2021-01 will show 0 or empty cell ?
Quentin
Super User

As @Kurt_Bremser mentioned, your picture doesn't match the code.  Can you show your full code (after the SQL step, are you transposing the data to move your counts into columns)?

 

PROC APPEND will not create any records/values for you.  It simply reads records from the DATA= dataset and appends them to the BASE= dataset.  

 

Also, PROC APPEND cannot add variables/columns to the base dataset.  So if you're you've got a base dataset with a column named NOV2021, and you use PROC APPEND to add records that have a new column DEC2021, the column won't be added. It will be ignored, or generate an error.

 

Generally it's a bad idea to store data in a wide format with weeks/months in columns.  It's preferable to keep the data in the vertical format your SQL query.

 

If you describe more of your goal, and show more code, I think people will be able to make better suggestions.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.
Kurt_Bremser
Super User

I don't deal in fairy tales. Please supply example data and the code you want to run against it, and the result you expect from that.

 

And if you ask íf something is possible, see Maxim 4.

ballardw
Super User

Proc append by default will not append any data set with different structure than the base data set.

 

If you use the FORCE option a data set missing a column or with "extra" columns may append but the extra columns won't be added. Or if the type of the variable is different than in base.

A small example.

data base;
   input x y z;
datalines;
1 2 3
;

data to_add;
  input x y q;
datalines;
44 55  66
;

/* default behavior no append and warning for 
   each difference in data set
*/
proc append base=base data=to_add;
run;

/*force option warning and variable q not added*/
proc append base=base data=to_add force;
run;

/* and if you end up with different data types*/

data to_add2;
   input x y z$;
datalines;
333 555 888
;

proc append base=base data=to_add2 force;
run;

If you have an empty data set to append no observations are added.

data to_add3;
   set base (obs=0);
run;
proc append base=base data=to_add3 force;
run;

The above creates an empty data set from the structure of the base data as an example.

 

Your suspect variable name "2021-11" indicate that Append is not what you want. And if you want 0 in the rows without the 'new' value or previous columns you have lots of work.

You probably should consider that this is a bad data structure because any analysis will require changing variable names in code in many places. You would like be better off with a data structure like

Run_date (if this is even needed) Number_Policy Effective_date (which would be a date value with the year month) Value (or what ever the number 2188  9078 etc means for each month).

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
  • 5 replies
  • 1902 views
  • 0 likes
  • 4 in conversation