Hi I have two variables with descriptive statistics and I need to calculate the difference between them
_name_ | var1 | var2 | var_want |
mean(sd) | 76.4(11.39) | 77.2(8.63) | |
min,max | 40.0,113.0 | 50.0,106.0 |
i've tried this,example
if _name_="min,max" then var_want=substr(var,1,2)-substr(var2,1,2)||","||substr(var,6,3)-substr(var2,6,3)
but it doesn't work cause 40-50=-10 and sas cannot convert it to char (invalid numeric data)
is there a simple way to calculate the difference between var1 and var2 ?
Maxim 2: Read the Log.
You will find NOTEs about automatic conversion of character to numeric and back.
To do reliable calculations, convert to numeric explicitly. The conversion back to character can be done implicitly by using the CATS function:
data have;
input _name_ :$10. var1 :$15. var2 :$15.;
datalines;
mean(sd) 76.4(11.39) 77.2(8.63)
min,max 40.0,113.0 50.0,106.0
;
data want;
set have;
if _name_="min,max"
then var_want = cats(
input(scan(var1,1,","),best.) - input(scan(var2,1,","),best.),
",",
input(scan(var1,2,","),best.) - input(scan(var2,2,","),best.)
);
run;
Also please note how I presented your data in a data step with datalines; please do so yourself in the future, as others can easily recreate a dataset with a simple copy/paste and submit, without any doubts about variable types, attributes and content.
Maxim 2: Read the Log.
You will find NOTEs about automatic conversion of character to numeric and back.
To do reliable calculations, convert to numeric explicitly. The conversion back to character can be done implicitly by using the CATS function:
data have;
input _name_ :$10. var1 :$15. var2 :$15.;
datalines;
mean(sd) 76.4(11.39) 77.2(8.63)
min,max 40.0,113.0 50.0,106.0
;
data want;
set have;
if _name_="min,max"
then var_want = cats(
input(scan(var1,1,","),best.) - input(scan(var2,1,","),best.),
",",
input(scan(var1,2,","),best.) - input(scan(var2,2,","),best.)
);
run;
Also please note how I presented your data in a data step with datalines; please do so yourself in the future, as others can easily recreate a dataset with a simple copy/paste and submit, without any doubts about variable types, attributes and content.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.