BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

 I have the  following dataset with Variables:

Have dataset:

Number -> Indicated the Actual Numerical Values

Dec        ->Number of digits (numbers) after the decimals have to be displayed.

Want Dataset:

Number Char --> Character format of "Number" after the decimals  format applied.

 data have;
 input Number dec ;
 cards;
 2.345  	2
 1.8    	1
 0      	2
 123.45   	1
 5.6781		2
 ;
run;

Want dataset: ( I  wrote the Numberchar ,rounding/  may have error)

SASuserlot_0-1706662626869.png

Thanks in Advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use the PUTN() function. That allows the format specification to be dynamically created.

data have;
  input Number dec ;
cards;
2.345   2
1.8     1
0       2
123.45  1
5.6781  2
;

data want;
  set have;
  length NumberChar $32 ;
  numberchar = left(putn(number,cats('32.',dec)));
run;

Result

                         Number
Obs     Number    dec     Char

 1       2.345     2     2.35
 2       1.800     1     1.8
 3       0.000     2     0.00
 4     123.450     1     123.5
 5       5.678     2     5.68

View solution in original post

5 REPLIES 5
SASKiwi
PROC Star
data have;
 input Number dec ;
 NumberChar = left(put(round(Number, 10 ** (-dec)), best12.));
cards;
2.345  2
1.8    1
0      2
123.45 1
5.6781 2
;
run;
Tom
Super User Tom
Super User

Use the PUTN() function. That allows the format specification to be dynamically created.

data have;
  input Number dec ;
cards;
2.345   2
1.8     1
0       2
123.45  1
5.6781  2
;

data want;
  set have;
  length NumberChar $32 ;
  numberchar = left(putn(number,cats('32.',dec)));
run;

Result

                         Number
Obs     Number    dec     Char

 1       2.345     2     2.35
 2       1.800     1     1.8
 3       0.000     2     0.00
 4     123.450     1     123.5
 5       5.678     2     5.68

SASuserlot
Barite | Level 11

Thank you both @Tom @SASKiwi. Both options worked. I learned something new function. I appreciate it.

 

@Tom, I have a similar question, but I'm not 100% sure how to ask the question.  I have the following dataset: "have." Is there any way  I can dynamically use the 'Symbol" variable to check "number" and "compare" and create a "CHECK" variable if the condition is correct or not?

have: ( usually I have the data I read from xlsx sheet)

data have;
number = 2;
symbol = '<=';
compare = 3;
output;
number = 2;
symbol = '>';
compare = 3;
output;
run;

SASuserlot_0-1706681031449.png

Want:

SASuserlot_1-1706681063614.png

 

ballardw
Super User

I suggest if you are going to go down this route that you do not use "symbols" but the mnemonic comparison operators such as EQ NE LT LE GT GE. It may be entirely too easy to get a unicode symbol that will not resolve to a valid comparison character.

 

This approach may work depending just how much junk you are going to attempt with this.

data junk;
  set have;
  select (symbol);
   when ('<=') result= number le compare;
   when ('>') result= number gt compare;
otherwise; end; run;

Result will be the typical 1 for true and 0 for false. I won't make any evaluation about "right" or "wrong" based on such logic. It would require a WHEN clause for each separate comparison you expect to do. If you do use the EQ NE etc I would suggest using Select (upcase(symbol)) to reduce the number of possibly entries in each When. When will use multiple choices or conditions separated by comma.

Ksharp
Super User

How about this one ?

 

data have;
number = 2;
symbol = '<=';
compare = 3;
output;
number = 2;
symbol = '>';
compare = 3;
output;
run;

data want;
set have;
want=ifc(resolve(cats('%sysevalf(',number,symbol,compare,',boolean)'))='1','Correct','Wrong  ');
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 590 views
  • 4 likes
  • 5 in conversation