BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

I need help in my sql code.  Some one helped the code. It worked but i have one more condition that i need to add.

 

I am getting output i need for all   where "var"   in  a,b,c,d,e,f.

 

But var=g   where i included a      "when value lt 1 then 0"     Please suggest. Thank you

Output needed for    var =g

var value prec

g     0.5  0

g     10   0

output getting

var value prec

g     0.5  0

g     10   1

 

data have;
input var $ value;
datalines;
a 100.01
a 110.003
a 100
b 1.1
b 2
c 5.000002
c 6.01
c 4
d 2
d 5
d 1120
e .
e 100
e 10.02
f .
f 10
g 0.5
g 10
;
proc sql;
create table want as
select 
    *,
    case when value =. then .
         when value lt 1 then 0 
        else max(lengthn(scan(put(value, best32.), 2, "."))) end as prec
from have
group by var;
quit;
2 REPLIES 2
yabwon
Amethyst | Level 16

Hi,

maybe this:

proc sql;
create table want as
select 
    var,
    value format best32.,
    case when value = . then .
         when value < 1 then 0 
         else max(case when value < 1 then 0
             else lengthn(scan(put(value, best32.), 2, ".")) end) 
         end as prec        
from have
group by var;
quit;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



mklangley
Lapis Lazuli | Level 10

What exactly are you trying to do with this code? Are you sure your results for a, b, c, d, e, and f are what you expect?

Is prec supposed to be the precision of value? (If so, how do you get prec = 6 for value = 4?)

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 714 views
  • 1 like
  • 3 in conversation