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

Hi I have two variables with descriptive statistics and I need to calculate the difference between them

_name_var1var2var_want
mean(sd)76.4(11.39)77.2(8.63) 
min,max40.0,113.050.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 ?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

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.

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
  • 1 reply
  • 1299 views
  • 0 likes
  • 2 in conversation