DATA Step, Macro, Functions and more

date format

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

date format

Hi All,

 

I want the dataset name to be created with year and month format,

 

ex.

 

ab002_201705 ,

 

data ab002_&sysyear&sysmonth is not giving desired result


Accepted Solutions
Solution
‎05-02-2017 03:08 AM
Super User
Posts: 6,928

Re: date format

First do the calculations in a data step, then save the result to a macro variable, and then use that for the dataset name:

data _null_;
myyear = year(today());
mymonth = month(today());
length string $6;
string = put(myyear,z4.) !! put(mymonth,z2.);
call symput('mymonth',string);
run;

%put "mymonth=&mymonth."; /* for control */

data ab002_&mymonth.;

This way you have full control over the formatting of the string.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎05-02-2017 03:08 AM
Super User
Posts: 6,928

Re: date format

First do the calculations in a data step, then save the result to a macro variable, and then use that for the dataset name:

data _null_;
myyear = year(today());
mymonth = month(today());
length string $6;
string = put(myyear,z4.) !! put(mymonth,z2.);
call symput('mymonth',string);
run;

%put "mymonth=&mymonth."; /* for control */

data ab002_&mymonth.;

This way you have full control over the formatting of the string.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 60

Re: date format

thank u , KurtBremsar
Super User
Super User
Posts: 7,392

Re: date format

Why.  All that will do is make your coding efforts difficult.  Either have one dataset with date as a column - this way you only have one dataset to program against but still have date information available - or have mutiple datasets, but call them something generic = ab002_1-ab002_xx - this way you can use lists of datasets, and short cuts to access them all (but having one dataset is far far simpler than anything else).

Do note that you might want "output" with the date on - this does not however define how you can work internally.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 116 views
  • 1 like
  • 3 in conversation