BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
talzy7
Calcite | Level 5

I have a dataset with variables Start_Date , End_Date, Price, and Unit. All the start dates begin at the first of the month and the end dates end on the last day of the month. In the existing data set the dates are for months in 2016. I would like to create new observations with the start date being 01JUL2017 and the end dates being 31JUL2017. These new July observations should have the same data as those whose start date is 01DEC2016. Any suggestions on how to create these new observations? Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

One way:

 

data want;

set have;

output;

if start_date = '01dec2016'd then do;

   start_date = '01jul2017'd;

   end_date = '31jul2017'd;

   output;

end;

run;

View solution in original post

19 REPLIES 19
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Take a copy of the data you want to copy, change it, add it back on.  Maybe something like (just a guess as you have not posted test data in the form of a datastep😞

data extr;
  set have;
  where date >= "01dec2016"d;
  /* move dates 7 months forward, so dec becomes jul etc. */
  date=intnx('month',date,7);
run;

/* Add onto existing data */
data want;
  set have extr;
run;
talzy7
Calcite | Level 5

Thank you!!

Vibcom
Fluorite | Level 6
can you please share the sample data?
talzy7
Calcite | Level 5

dataset.png

 

This is the first seven observations of my initial data set.

Astounding
PROC Star

One way:

 

data want;

set have;

output;

if start_date = '01dec2016'd then do;

   start_date = '01jul2017'd;

   end_date = '31jul2017'd;

   output;

end;

run;

talzy7
Calcite | Level 5

Thank you!! This was very helpful!

talzy7
Calcite | Level 5

In my data step I am trying to create a new variable called Season which is Winter if start_date = '01DEC2016'd and is summer if start_date='01JUL2017'd. I tried the following in my data step but the season column is not filled.

 

select (Start_Date);
when ('01DEC2016'd) do;
Season = 'Winter';
output work.extended;
end;
when ('01JUL2017'd) do;
Season = 'Summer';
output work.extended;
end;
otherwise do;
end;
end;

Astounding
PROC Star

You say you want to do something if the start date is July 1, 2016.  But your program never checks for the start date being July 1, 2016.

talzy7
Calcite | Level 5

Does select (Start_Date) and when not do this?

Astounding
PROC Star

That will look at start_date.  But none of your WHEN clauses look for that particular date.  So nothing happens for that date.

talzy7
Calcite | Level 5

What is the best method to use to check for the date?

Astounding
PROC Star

The method you are using is fine.  These particular WHEN statements just never check for the date that you are interested in.

 

It's also not clear if you mean to add the OUTPUT statements, or whether they should just be omitted.  That depends on what you want the result to be.

talzy7
Calcite | Level 5

This is the data step I am currently trying:

 

data work.extended;
set prg.discount2016;
output;
where Start_Date = '01DEC2016'd;
if Start_Date = '01DEC2016'd then do;
Promotion = 'Holiday Bonus';
output;
end;
if Start_Date = '01DEC2016'd then do;
Start_Date = '01JUL2017'd;
End_Date = '31JUL2017'd;
output;
end;
drop Unit_Sales_Price;

select (Start_Date);
when ('01DEC2016'd) do;
Season = 'Winter';
output work.extended;
end;
when ('01JUL2017'd) do;
Season = 'Summer';
output work.extended;
end;
end;
run;

 

I am still not understanding why it wont output the season if I explicitly say when ('01DEC2016'd) ? 

Astounding
PROC Star

Your DATA step statements execute in order, from top to bottom.  

 

Earlier statements change the start date from 01DEC2016 to 01JUL2017.  Those changes remain in effect, so when you later check for 01DEC2016 there is no match.  The value has been changed to 01JUL2017.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 19 replies
  • 1479 views
  • 0 likes
  • 4 in conversation