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

hello:  why the following doesn't work?

data have;

input N1 best12.3;

cards;

542.

742.268

524.

742.39

;

run;

data want;

set have;

if N1='742' then substr(((put(N1,7.)), 1, 3) = '920';

run;

 

180 data want;

181 set have;

182 if N1='742' then substr(((put(N1,7.)), 1, 3) = '920';

                                                               -

                                                             79

                                              -

                                            356

ERROR 79-322: Expecting a ).

NOTE 356-185: The SUBSTR pseudo-variable function does not allow character constants, expressions, or

numeric constants for the first argument.

183 run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

I'll go with a slightly different variation:

 

if int(n1)=742 then n1 = n1 + 178;

 

That preserves the decimal fraction so increases from (for example) 742.123 to (for example) 920.123

 

SUBSTR only applies to character strings.  So if by chance N1 were actually a character variable, you could use:

 

if n1 =: '742.' then substr(n1, 1, 3) = '920';

View solution in original post

17 REPLIES 17
Reeza
Super User

You have 4 open parentheses '(', and 3 closing parentheses ')' (like an earlier question). When you see that error, count the number of open/close parentheses. There are also keyboard shortcuts to jump to the matching closing/opening parentheses, but the error is telling you what's wrong, and exactly where. 

 

SUBSTR can be used in LEFT operation to replace text but I suspect that's not what you're trying to do. I don't know what you're trying to do though. 

 

if N1='742' then substr((put(N1,7.), 1, 3) = '920';

 

 

ybz12003
Rhodochrosite | Level 12

Try to change 742 to 920

Reeza
Super User

@ybz12003 wrote:

Try to change 742 to 920


Then why are you using SUBSTR in your THEN?

Your condition will only be met if the number is exactly 742. 

 

I think your logic is backwards in your IF statement. I suggest literally deleting this one and trying to write your IF from scratch. 

 

 

 

Reeza
Super User

I think you want this:

 

if int(n1)=742 then n1= n1-int(n1) + 920;

You can do the character conversion. 

ybz12003
Rhodochrosite | Level 12

I still get the same error message even though I use if N1='742' then substr((put(N1,7.), 1, 3) = '920';

art297
Opal | Level 21

Is this what you are trying to do?:

data have;
  input N1 best12.3;
  cards;
542.
742.268
524.
742.39
;
run;

data want;
  set have;
  if int(N1) eq 742 then n1 = 920;
run;

Art, CEO, AnalystFinder.com

 

Reeza
Super User

@art297 It's 20 questions time, and I think I win 😉

art297
Opal | Level 21

I beat you by 16 seconds! 🙂

Reeza
Super User

@art297 wrote:

I beat you by 16 seconds! 🙂


Yeah, but your missing the decimal portion, and I think that's what he was trying to do, replace the integer portion. Mind you, it's still a guess....

ybz12003
Rhodochrosite | Level 12

No, I would like the final data is like

 

data have;

input N1 best12.3;

cards;

542.

920.268

524.

920.39

;

run;

art297
Opal | Level 21

Then I agree with @Reeza's most recent suggestion.

 

@Reeza: You win!

 

ybz12003
Rhodochrosite | Level 12

I have other similar rows need to changed, something like 789.456 to 234.456.  Is there a way no need to be calculated?

Reeza
Super User

@ybz12003 wrote:

I have other similar rows need to changed, something like 789.456 to 234.456.  Is there a way no need to be calculated?


Probably, but you'd have to tell us more. I'm tired of playing 20 questions tonight. 

Astounding
PROC Star

I'll go with a slightly different variation:

 

if int(n1)=742 then n1 = n1 + 178;

 

That preserves the decimal fraction so increases from (for example) 742.123 to (for example) 920.123

 

SUBSTR only applies to character strings.  So if by chance N1 were actually a character variable, you could use:

 

if n1 =: '742.' then substr(n1, 1, 3) = '920';

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
  • 17 replies
  • 4479 views
  • 7 likes
  • 5 in conversation