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;
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';
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';
Try to change 742 to 920
@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.
I think you want this:
if int(n1)=742 then n1= n1-int(n1) + 920;
You can do the character conversion.
I still get the same error message even though I use if N1='742' then substr((put(N1,7.), 1, 3) = '920';
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
@art297 It's 20 questions time, and I think I win 😉
I beat you by 16 seconds! 🙂
@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....
No, I would like the final data is like
data have;
input N1 best12.3;
cards;
542.
920.268
524.
920.39
;
run;
I have other similar rows need to changed, something like 789.456 to 234.456. Is there a way no need to be calculated?
@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.
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';
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.