Hi,
I am learning about find and compress functions. There is a variable called weight in a set learn.study. That variable has weights in both Kgs and lbs. I want to convert all the weights into pounds, and make it numeric. I have tried doing this, but get an error, and I am not sure what is wrong. Any help would be appreciated. Thanks.
Below is the code:
libname Learn '/folders/myfolders/Learn' ;
Data Prob12_8 ;
Set learn.study(rename = (Weight = WeightTemp)) ;
If find(weighttemp, kg, 'i') then weight = 2.2*input(compress(Weighttemp,,'kd', 8. ));
else if find(weighttemp, lbs, 'i') then weight = input(compress(Weighttemp,,'kd', 8. )) ;
proc print data=Prob12_8 noobs ;
run ;
Check your parentheses, I think the closing ones are in the wrong places.
Just a general principle at work ... in a DATA step, you need quotes to indicate that a set of characters is a character string and not a reference to a variable. Add quotes around "lbs" and "kg".
Thanks for that suggestion. I have corrected that. However, I am still getting this error in the log:
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, ',', -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE,
GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.
Here is the code:
libname Learn '/folders/myfolders/Learn' ;
Data Prob12_8 ;
Set learn.study(rename = (Weight = WeightTemp)) ;
If find(weighttemp, 'kg', 'i') then weight = 2.2*input(compress(Weighttemp,,'kd', 8. ));
else if find(weighttemp, 'lbs', 'i') then weight = input(compress(Weighttemp,,'kd', 8. )) ;
proc print data=Prob12_8 noobs ;
run ;
When you get an error post the code and error message from log. Paste it into a codebox opened using the forum {i} menu icon to preserve formatting as the main message windows reformat text and will move the indicator, an _ , that appears at the offending postion in the code.
I am pretty sure the problem is here:
If find(weighttemp, 'kg', 'i') <there should be a comparison value> then weight = 2.2*input(compress(Weighttemp,,'kd', 8. ));
such as
If find(weighttemp, 'kg', 'i') >0 then weight = 2.2*input(compress(Weighttemp,,'kd', 8. ));
and will be needed for both IF statements.
Hi, I did use the > 0 as recommended, but I am still getting the same error. Below is the code:
libname Learn '/folders/myfolders/Learn' ; Data Prob12_8 ; Set learn.study(rename = (Weight = WeightTemp)) ; If find(weighttemp, 'kg', 'i') > 0 then weight = 2.2*input(compress(Weighttemp,,'kd', 8.)) ; else if find(weighttemp, 'lbs', 'i') > 0 then weight = input(compress(Weighttemp,,'kd', 8.)) ; proc print data=Prob12_8 noobs ; run ;
----------------
Check your parentheses, I think the closing ones are in the wrong places.
@Reeza hit the nail on the head, I believe. You have:
If find(weighttemp, 'kg', 'i') then weight = 2.2*input(compress(Weighttemp,,'kd', 8. ));
else if find(weighttemp, 'lbs', 'i') then weight = input(compress(Weighttemp,,'kd', 8. )) ;
This should work:
If find(weighttemp, 'kg', 'i') then weight = 2.2*input(compress(Weighttemp,,'kd'), 8. );
else if find(weighttemp, 'lbs', 'i') then weight = input(compress(Weighttemp,,'kd'), 8. ) ;
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.