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

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 ;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

View solution in original post

6 REPLIES 6
Astounding
PROC Star

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

ManitobaMoose
Quartz | Level 8

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 ;

ballardw
Super User

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.

ManitobaMoose
Quartz | Level 8
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
Reeza
Super User

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

Astounding
PROC Star

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 6 replies
  • 1187 views
  • 0 likes
  • 4 in conversation