Hello community,
i have a problem in creating a date variable
for example i want to create a date variable from 1983 to 2017 using this code
DATA datatime ; format date year4. ; do date= '1983'd to '2017'd ; output datatime ; end ; run
however, i face an error.
thank you for your helps
SAS dates represent a single day, so you need to supply a complete date if using date literals:
%let start=1983;
%let end=2017;
data datatime;
format date year4. ;
date = "01jan&start."d;
do until (date > "01jan&end."d);
output datatime;
date = intnx('year',date,1);
end;
run;
Alternatively, using the iterative do loop:
data datatime;
format date year4.;
do year = &start to &end;
date = mdy(1,1,year);
output;
end;
drop year;
run;
A SAS numeric date can only be all three components as it is a count of days since the cuttoff point. 1983 is not a valid "date", it is just a number. So you would need to create actual dates (doesn't matter what format you apply) and display it differently:
data datatime (drop=i); format date year4.; do i=1983 to 2017;
date=mdy(1,1,i); output; end; run;
This will create 34 rows, one for each year with the date variable containing 01JAN for each year, the format then just shows the year part only (but if you remove the format you will see this).
To be honest though you do not need to use a format, or a date, unless you really need to as:
data datatime; do year=1983 to 2017;
output; end; run
Is effectively the same.
Hello,
i used your suggestion
but after creating the date variable i can't plot data
When i applied this code
proc timeseries data=data out=series; id date interval=year; var date D; run;
i got only this table
And what exactly is it you want? This new information has nothing to do with the original question. For plots you specify plots on the proc line, and you need to have an ods destination open:
http://support.sas.com/documentation/cdl/en/etsug/63939/HTML/default/viewer.htm#etsug_timeseries_sec...
The code you give doesn't seem to plot anything, just creates and output dataset of the results.
Sorry, how does this post relate to "i used your suggestion but after creating the date variable i can't plot data"?
If you want to see if a variable is created, you first check the log - which is your one source of the run - then you check the dataset. We can only provide guidance on the information you provide in the post.
@Lok07 wrote:
I want just verify if my variable was created or not.
Inspect the log.
Look at your dataset with the table viewer.
Use proc contents.
Use proc datasets.
Retrieve information from dictionary.columns or sashelp.vcolumn.
Do a proc print without the var statement.
If the variable is present, run a proc freq to see the distribution of values.
Need any more options?
It works
Thank you so much for your help
SAS Dates must include a day of the month and a month and a year. Yours only includes year.
You need to use something like
'01JAN1983'd
OR
DATA datatime ;
do date= 1983 to 2017 ;
output datatime ;
end ;
run;
SAS dates represent a single day, so you need to supply a complete date if using date literals:
%let start=1983;
%let end=2017;
data datatime;
format date year4. ;
date = "01jan&start."d;
do until (date > "01jan&end."d);
output datatime;
date = intnx('year',date,1);
end;
run;
Alternatively, using the iterative do loop:
data datatime;
format date year4.;
do year = &start to &end;
date = mdy(1,1,year);
output;
end;
drop year;
run;
You need to give date value like '01jan2018'd instead of just '2018'd. When you give actual date values and need by each year you need to loop by year, for this you can use INTNX to go to next year.
In the below code you can also change the format for the date and also the loop to months or days instead of year.
DATA datatime ;
format date year4.;
do date='01JAN1983'd to '31JAN2017'd;
output;
date=intnx('year',date,1,"b");
end;
run;
If you want to give numeric values instead of giving the actual dates then this might work.
DATA datatime ;
format date year4.;
do i=1983 to 2017;
date=input(cats("01jan",i),date9.);
output;
;
end;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.