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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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