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

 

/*how to add a calculated variable to our code? I want o add a variable which shows num1/ num2, call it rate*/
/* boxes doesn't allow to key in our questions*/

options validvarname=any;

data mydate;
   infile datalines dlm=',';
   length 'health plan'n $40 'sub health plan'n $30 'diag code'n $10;
   input 'health plan'n 'sub health plan'n 'diag code'n num1 num2;
   datalines;
Marina hills county, Misson plan, 234, 110, 20
;

 

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

It can be written as follows.
(The case where num2 is 0 or missing is not taken into account.)

 

data mydate;
   infile datalines dlm=',';
   length 'health plan'n $40 'sub health plan'n $30 'diag code'n $10;
   input 'health plan'n 'sub health plan'n 'diag code'n num1 num2;
   rate=num1/num2;
   datalines;
Marina hills county, Misson plan, 234, 110, 20
;
run;

View solution in original post

10 REPLIES 10
japelin
Rhodochrosite | Level 12

It can be written as follows.
(The case where num2 is 0 or missing is not taken into account.)

 

data mydate;
   infile datalines dlm=',';
   length 'health plan'n $40 'sub health plan'n $30 'diag code'n $10;
   input 'health plan'n 'sub health plan'n 'diag code'n num1 num2;
   rate=num1/num2;
   datalines;
Marina hills county, Misson plan, 234, 110, 20
;
run;
andreas_lds
Jade | Level 19

Note: don't use name-literals if you are not forced to used them. They are worsening the readability of your code.

GN0001
Barite | Level 11
Thanks for the response.
What is name literals?
Regards,
Blue Blue
Blue Blue
GN0001
Barite | Level 11

Sometimes,

We don't have a control over the external world, we might receive a file that has spaces in first observation.

Thanks for the response.

Blue blue

 

Blue Blue
Kurt_Bremser
Super User

Don't make your life hard by using those name literals. Use underlines in place of blanks:

data mydate;
   infile datalines dlm=',';
   length health_plan $40 sub_health_plan $30 diag_code $10;
   input health_plan sub_health_plan diag_code num1 num2;
   datalines;
Marina hills county, Misson plan, 234, 110, 20
;

See how much easier to read (and type!) that code is?

Kurt_Bremser
Super User

And the you can improve the readability even more by moving the formats into the INPUT statement, and structure it visually:

data mydate;
infile datalines dlm=',';
input
  health_plan :$40.
  sub_health_plan :$30.
  diag_code :$10.
  num1
  num2
;
datalines;
Marina hills county, Misson plan, 234, 110, 20
;

Having items on separate lines makes editing the code easier (shuffling items can be done my marking/moving whole lines, which is less work than having to exactly move the cursor to the needed columns within a line).

GN0001
Barite | Level 11

Thanks for the response. I will try to adapt it.

How to show the calculated field?

Regards,

Blue blue

Blue Blue

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
  • 10 replies
  • 755 views
  • 6 likes
  • 5 in conversation