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

Why does this produce .03 instead of 3? 

proc sql;
create table test (
graduation_period varchar(6),
cohort varchar(6)
);

insert into test (graduation_period, cohort) values ("201120", "200810");
quit;
data outputTable; set test;
time_to_graduation = input(substr(graduation_period, 1,4), 6.2) - input(substr(cohort, 1,4), 6.2);
run;

 

I'm trying to run this after.

 

proc sql;
create table test (
graduation_period varchar(6),
cohort varchar(6)
);

insert into test (graduation_period, cohort) values ("201120", "200810");
quit;
data outputTable; set test;
time_to_graduation = input(substr(graduation_period, 1,4), 6.2) - input(substr(cohort, 1,4), 6.2);

if(time_to_graduation * 100 - input(graduation_period, 8.) - input(cohort, 8.) = 90) then time_to_graduation = time_to_graduation + .33;
if(time_to_graduation * 100 - input(graduation_period, 8.) - input(cohort, 8.) = 20) then time_to_graduation = time_to_graduation + .33;
if(time_to_graduation * 100 - input(graduation_period, 8.) - input(cohort, 8.) = 80) then time_to_graduation = time_to_graduation + .66;
if(time_to_graduation * 100 - input(graduation_period, 8.) - input(cohort, 8.) = 10) then time_to_graduation = time_to_graduation + .66;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
DavidPhillips2
Rhodochrosite | Level 12

This worked

 

 

time_to_graduation = input(graduation_period, 4.) - input(cohort, 4.);
if(input(graduation_period, 6.) - input(cohort, 6.) - time_to_graduation * 100) = 10 then do;
time_to_graduation = time_to_graduation + .66;
end;

View solution in original post

6 REPLIES 6
art297
Opal | Level 21

You're forcing the last two digits to be a fraction. Why not just use:

data outputTable; set test;
  time_to_graduation = input(substr(graduation_period, 1,4), 4.) - input(substr(cohort, 1,4), 4.);
run;

Art, CEO, AnalystFinder.com

 

DavidPhillips2
Rhodochrosite | Level 12

That works for that line but,  I need to add the value from that to the fraction on the next part.

Astounding
PROC Star

That's the right issue.  And as long as you are doing that, you don't need SUBSTR:

 

time_to_graduation = input(graduation_period, 4.) - input(cohort, 4.);

DavidPhillips2
Rhodochrosite | Level 12

How do I convert it so the next line will work with it?

 

 

time_to_graduation = input(graduation_period, 4.) - input(cohort, 4.);
if(time_to_graduation * 100 - input(graduation_period, 8.) - input(cohort, 8.) = 90) then time_to_graduation = time_to_graduation + .33;

DavidPhillips2
Rhodochrosite | Level 12

This worked

 

 

time_to_graduation = input(graduation_period, 4.) - input(cohort, 4.);
if(input(graduation_period, 6.) - input(cohort, 6.) - time_to_graduation * 100) = 10 then do;
time_to_graduation = time_to_graduation + .66;
end;

ballardw
Super User

You might provide exactly what you are attempting to do. Since this looks like date manipulation and comparisons then it may be that intck and/or intnx with some actual date values instead with offsets may be of use. Though we would need some explanation of what 201120 and 200810 actually represent. The first four digits look like years but the 20 in 201120 is not intuitively obvious.

 


@DavidPhillips2 wrote:

How do I convert it so the next line will work with it?

 

 

time_to_graduation = input(graduation_period, 4.) - input(cohort, 4.);
if(time_to_graduation * 100 - input(graduation_period, 8.) - input(cohort, 8.) = 90) then time_to_graduation = time_to_graduation + .33;


The 8 format isn't going to do will reading 6 characters...

Your calculation has a number of magic numbers. What is the purpose of the 100 and the .33?

As a minimum I think we need at least 2 if not a couple more worked examples showing the expected result.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 2238 views
  • 0 likes
  • 4 in conversation