DATA Step, Macro, Functions and more

Find and Compress function difficulty

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Find and Compress function difficulty

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 ;


Accepted Solutions
Solution
‎09-15-2017 08:32 PM
Super User
Posts: 19,878

Re: Find and Compress function difficulty

Posted in reply to ManitobaMoose

Check your parentheses, I think the closing ones are in the wrong places.

View solution in original post


All Replies
Super User
Posts: 5,518

Re: Find and Compress function difficulty

Posted in reply to ManitobaMoose

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".

Contributor
Posts: 26

Re: Find and Compress function difficulty

Posted in reply to Astounding

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 ;

Super User
Posts: 11,343

Re: Find and Compress function difficulty

Posted in reply to ManitobaMoose

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.

Contributor
Posts: 26

Re: Find and Compress function difficulty

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 ;



----------------
 
 
 
 
Below is the log:
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
61
62 /* Chapter 12*/
63 /* Problem 12.8*/
64 libname Learn '/folders/myfolders/Learn' ;
NOTE: Libref LEARN was successfully assigned as follows:
Engine: V9
Physical Name: /folders/myfolders/Learn
65
66 Data Prob12_8 ;
67 Set learn.study(rename = (Weight = WeightTemp)) ;
68 If find(weighttemp, 'kg', 'i') > 0 then weight = 2.2*input(compress(Weighttemp,,'kd', 8.)) ;
_
22
76
69 else if find(weighttemp, 'lbs', 'i') > 0 then weight = input(compress(Weighttemp,,'kd', 8.)) ;
_
22
76
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, ^, ^=, |, ||, ~, ~=.
 
ERROR 76-322: Syntax error, statement will be ignored.
 
70
 
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.PROB12_8 may be incomplete. When this step was stopped there were 0 observations and 6 variables.
WARNING: Data set WORK.PROB12_8 was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
71 proc print data=Prob12_8 noobs ;
 
72 run ;
 
NOTE: No observations in data set WORK.PROB12_8.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
73
74
75 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
88
Solution
‎09-15-2017 08:32 PM
Super User
Posts: 19,878

Re: Find and Compress function difficulty

Posted in reply to ManitobaMoose

Check your parentheses, I think the closing ones are in the wrong places.

Super User
Posts: 5,518

Re: Find and Compress function difficulty

@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. ) ;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 252 views
  • 0 likes
  • 4 in conversation