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.
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;
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;
Thank you!!
This is the first seven observations of my initial data set.
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;
Thank you!! This was very helpful!
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;
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.
Does select (Start_Date) and when not do this?
That will look at start_date. But none of your WHEN clauses look for that particular date. So nothing happens for that date.
What is the best method to use to check for the date?
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.
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) ?
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.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: