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

Hi,

 

I am looking to convert a character variable (YEAR_MONTH) to a numeric variable, which has been written in the code below. Please can someone change the line of code so that the dataset converts the variable and filter my dataset so that only observations are kept when the newly converted YEAR_MONTH numeric variable is less than or equal to 30 June 2018? Would it be possible to do this in one DATA step, or does it have to be done in two datasets (convert variable in first DATA step and then subset dataset in second DATA step)?

 

Code:

Where to include?

YEAR_MONTH = input(YEAR_MONTH,6.);

 

data test;
   set test2;
   if YEAR_MONTH le '30Jun2018'd;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Why not to use WHERE clause?

 

data test;
   set test2;
   where input(year_month,yymmn6.) le '30Jun2018'd;
run;

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@jeremy4 wrote:

I am looking to convert a character variable (YEAR_MONTH) to a numeric variable,


Is that 2018_06 (for example) or 201806?

 

If the latter, then

 

data test;
   set test2;
   ym=input(year_month,yymmn6.);
   if ym le '30Jun2018'd;
run;

 

 

--
Paige Miller
yabwon
Onyx | Level 15

Why not to use WHERE clause?

 

data test;
   set test2;
   where input(year_month,yymmn6.) le '30Jun2018'd;
run;

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



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!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 309 views
  • 2 likes
  • 3 in conversation