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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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