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

Hi ,

I have a dataset which has rpflag , rp1,rp2,rp3,rp4 amounts in numeric format . I want to create rp5 column 

 

case when rpflag = 'Y' then (TRIM(put(rp1,8.)))||'/'||(TRIM(put(rp2,8..)))
when rpflag = 'N' then (TRIM(put(rp3,8.)))||'/'||(TRIM(put(rp4,8.)))
else '' end as rp5

 

RPFlag RP1RP2RP3RP4RP5
Y20350020/35
N00203020/30
N00000/0
Y00000/0
Y56330056/33
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Make sure to read the log and include in future posts.

 

You have two periods in the PUT statement. 

 

 

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 68         
 69         
 70         proc sql;
 71         create table want as
 72         Select *,
 73         case when rpflag = 'Y' then (TRIM(put(rp1,8.)))||'/'||(TRIM(put(rp2,8..)))
                                                                                  _
                                                                                  22
                                                                                  200
 ERROR 22-322: Syntax error, expecting one of the following: ), -.  
 
 ERROR 200-322: The symbol is not recognized and will be ignored.
 

That's fairly complicated coding though - using CATX() is preferable - it trims automatically and adds in the / in between. 

This is cleaner IMO:

 

case when rpflag = 'Y' then catx('/', put(rp1, 8.), put(rp2, 8.))
when rpflag = 'N' then catx('/', put(rp1, 8.), put(rp2, 8.))
else '' end as rp6

@jhh197 wrote:

Hi ,

I have a dataset which has rpflag , rp1,rp2,rp3,rp4 amounts in numeric format . I want to create rp5 column 

 

case when rpflag = 'Y' then (TRIM(put(rp1,8.)))||'/'||(TRIM(put(rp2,8..)))
when rpflag = 'N' then (TRIM(put(rp3,8.)))||'/'||(TRIM(put(rp4,8.)))
else '' end as rp5

 

RPFlag  RP1 RP2 RP3 RP4 RP5
Y 20 35 0 0 20/35
N 0 0 20 30 20/30
N 0 0 0 0 0/0
Y 0 0 0 0 0/0
Y 56 33 0 0 56/33

 

View solution in original post

7 REPLIES 7
jhh197
Pyrite | Level 9

Can anyone please help thank you 

PeterClemmensen
Tourmaline | Level 20

Does it have to be SQL code?

Reeza
Super User

Make sure to read the log and include in future posts.

 

You have two periods in the PUT statement. 

 

 

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 68         
 69         
 70         proc sql;
 71         create table want as
 72         Select *,
 73         case when rpflag = 'Y' then (TRIM(put(rp1,8.)))||'/'||(TRIM(put(rp2,8..)))
                                                                                  _
                                                                                  22
                                                                                  200
 ERROR 22-322: Syntax error, expecting one of the following: ), -.  
 
 ERROR 200-322: The symbol is not recognized and will be ignored.
 

That's fairly complicated coding though - using CATX() is preferable - it trims automatically and adds in the / in between. 

This is cleaner IMO:

 

case when rpflag = 'Y' then catx('/', put(rp1, 8.), put(rp2, 8.))
when rpflag = 'N' then catx('/', put(rp1, 8.), put(rp2, 8.))
else '' end as rp6

@jhh197 wrote:

Hi ,

I have a dataset which has rpflag , rp1,rp2,rp3,rp4 amounts in numeric format . I want to create rp5 column 

 

case when rpflag = 'Y' then (TRIM(put(rp1,8.)))||'/'||(TRIM(put(rp2,8..)))
when rpflag = 'N' then (TRIM(put(rp3,8.)))||'/'||(TRIM(put(rp4,8.)))
else '' end as rp5

 

RPFlag  RP1 RP2 RP3 RP4 RP5
Y 20 35 0 0 20/35
N 0 0 20 30 20/30
N 0 0 0 0 0/0
Y 0 0 0 0 0/0
Y 56 33 0 0 56/33

 

Tom
Super User Tom
Super User

CATX() also does not require you to convert your numbers to strings. It will convert them automatically.

case when rpflag = 'Y' then catx('/', rp1,rp2)
     when rpflag = 'N' then catx('/', rp3,rp4)
     else ' ' 
end as rp6
jhh197
Pyrite | Level 9

Thank you everyone for helping 🙂

Reeza
Super User

@Tom wrote:

CATX() also does not require you to convert your numbers to strings. It will convert them automatically.

case when rpflag = 'Y' then catx('/', rp1,rp2)
     when rpflag = 'N' then catx('/', rp3,rp4)
     else ' ' 
end as rp6

True and even simpler!

jhh197
Pyrite | Level 9

Thank you so much it worked 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 471 views
  • 5 likes
  • 4 in conversation