BookmarkSubscribeRSS Feed
PIEFLAVOR
Calcite | Level 5

I am trying to convert a text response containing monthly to a weekly value. The issue is when I run the code below it divides all values in var2 instead of just the ones that meet the text response requirements (e.g. 2 month) . Hopefully this makes sense, please let me know if my issue is unclear.

 

data have;

IDvar1var2
12 months150
2two months90
31 week30
41 week45
52 months100
62 months220

 

Desired outcome=

IDvar1var2
11 week17.26
21 week10.36
31 week30
41 week45
51 week11.5
61 week25.31

 

Current code

data want;
set have;
 
if index(lowcase(var1), '2 month') > 0 then var2 = var2/8.69;
else if index(lowcase(var1), '2 months') > 0 then var2 = var2/8.69;
else if index(lowcase(var1), 'two months') > 0 then var2 = var2/8.69;
run;
5 REPLIES 5
Patrick
Opal | Level 21

Your question will become much clearer if you provide us with sample HAVE data and show us the desired result.

 

The code you shared returns the result you desire as far as I understand your question.

data have;
  infile datalines truncover dlm=',';
  input var1 $50.;
  var2=8.69049;
  datalines;
xxx 2 Month yyy
xxx 2 Months yyy
xxx two Months yyy
xxx 3 Months yyy
;

data want;
  set have;
  if index(lowcase(var1), '2 month') > 0 then var2 = var2/8.69049;
  else if index(lowcase(var1), '2 months') > 0 then var2 = var2/8.69049;
  else if index(lowcase(var1), 'two months') > 0 then var2 = var2/8.69049;
run;

proc print data=want;
run;

Patrick_0-1706147388852.png

 

 

Astounding
PROC Star
Note that you don't need to look for:
"2 months"
Any time it appears, it would have been caught by the first check, looking for:
"2 month"
PIEFLAVOR
Calcite | Level 5

I forgot to include my current results (undesired)-

IDvar1var2
11 week17.26
21 week10.36
31 week3.45
41 week5.17
51 week11.5
61 week25.31
ballardw
Super User

I am not sure that I see a clear definition of a conversion from "month" to week. This may be very important if you have values of 3 or more months. I see one conversion for only 2 months. Do you have more variability in your data?

mkeintz
PROC Star

I guess you assume weeks per month =  4.34523809523809  (= 365/*(7*12)).

 

If you have only two original time units ("week/weeks"  or "month/months") then discriminating between them is easy.  The remaining task is to get a numeric value from the first "word" in var1.  You could then do:

 

data have;
  input var1 $12.   var2;
datalines;
2 months     150
two months   90
1 week       30
1 week       45
2 months     100
2 months     220
run;

%let weeks_per_month=%sysevalf((365/7)/12);

data want (/*drop=_:*/);
  set have;
  array numwords {12} $6 _temporary_ ('one','two','three','four','five','six',
                                      'seven','eight','nine','ten','eleven','twelve');
  _nunits=whichc(lowcase(scan(var1,1)),of numwords{*});
  if _nunits=0 then _nunits=input(scan(var1,1),2.);

  if index(lowcase(var1),'month') then weekly_var2=  var2/(_nunits*&weeks_per_month);
  else weekly_var2=var2/_nunits;
run;

This allows you to have "one month", "two months", ... "twelve months" to start var1 (or "one week" through "twelve weeks"), as well as any set of (up to) 12 digits.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 518 views
  • 2 likes
  • 5 in conversation