DATA Step, Macro, Functions and more

creating a variable

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

creating a variable

Hi,

 

I have the following data-set  with first three variables, and I want to create another variable month such that it represents the month and year of the date variable. Any suggestions on how to create it?

winidpermnodatemonth
111/1/2000Jan-00
111/2/2000Jan-00
111/3/2000Jan-00
11…..Jan-00
11…..Jan-00
11…..Jan-00
111/31/2000Jan-00
212/1/2000Feb-00
212/2/2000Feb-00
21….Feb-00
21…..Feb-00
212/29/2000Feb-00

Accepted Solutions
Solution
4 weeks ago
Super User
Super User
Posts: 9,599

Re: creating a variable

Not sure how mabny ways there is to type this: Post test data in the form of a datastep!!!

So now lets start the guessing, is date an actual numeric date value?  If so then:

data want;
  set have;
  month=put(date,monyy.);
run;

http://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#n1anshj9oarq...

View solution in original post


All Replies
Solution
4 weeks ago
Super User
Super User
Posts: 9,599

Re: creating a variable

Not sure how mabny ways there is to type this: Post test data in the form of a datastep!!!

So now lets start the guessing, is date an actual numeric date value?  If so then:

data want;
  set have;
  month=put(date,monyy.);
run;

http://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#n1anshj9oarq...

Super User
Posts: 13,508

Re: creating a variable

For a very large number of analysis or display purposes what you propose doing may not be needed at all. If the "date" is an actual SAS date value and not character then a change of the display format for a procedure may be all that is needed. See this example:

 

data example;
   informat date mmddyy10. ;
   format date mmddyy10.;
   input date value;
datalines;
01/01/2018  123
01/23/2018  24
01/31/2018  456
02/02/2018  333
02/05/2018  6
;
run;

proc means data=example;
   class date;
   var value;
run;

proc means data=example;
   class date;
   var value;
   format date monyy5.;
run;

No additional variable was needed to get different groups for analysis, just a change of format.

 

Almost all of the analysis, grahping and reporting procedures like Report and Tabulate will honor the format for creating groups.

Also Proc Format will allow you create specific date, time or datetime appearances if you can't find a SAS supplied format that appears exactly as you want.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 70 views
  • 1 like
  • 3 in conversation