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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.   

Lok07
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

Lok07
Calcite | Level 5
I want just verify if my variable was created or not.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Kurt_Bremser
Super User

@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?

Lok07
Calcite | Level 5

It works

Thank you so much for your help

PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
Kurt_Bremser
Super User

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;
SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

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.

Discussion stats
  • 10 replies
  • 2221 views
  • 4 likes
  • 5 in conversation