/*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
;
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;
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;
Note: don't use name-literals if you are not forced to used them. They are worsening the readability of your code.
health_plan
is a "valid SAS name" (only letters, digits and underlines, does not start with a digit)
"health plan"n
is a "name literal", necessary because of the blank.
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
Irrelevant. When reading CSV (or other text) files, you skip the header line with FIRSTOBS=2 and define the variable names in the INPUT statement. Do not use PROC IMPORT.
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?
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).
Thanks for the response. I will try to adapt it.
How to show the calculated field?
Regards,
Blue blue
Read @japelin 's post.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.