I am trying to understand the working of percent format in SAS.
data a;
a=put(1.1,percent4.);
run;
O/P:-
**%
I am expecting the output to return 110%.
I get the expected result when I use the following code:-
data a;
a=put(1.1,percent6.);
run;
Can anyone explain why I didn't get the expected result as the width of the o/p is 4 which shall be fulfilled by percent4. ?
From the documentation: The width of the output field must account for the percent sign (% )
and parentheses for negative numbers, whether the number is negative
or positive.
So for a percent value you always need 3 + w (the width) you would like displayed. In your case, you have 3, so 3+3 = minimum 6 to have it displayed correctly.
Try this way...
data a;
ab=put(1.100,percent6.2);
run;
Make sense Dragonthoughts...
I didn't read that the problem is return 110% with percent4.0
Reeza have aswered yet.
Related
Hello I need to add a zero for only results and ranges are in a .2, .3 etc format but not tall values are like this. What would be the most efficient way to do this?.
I've only tried to format it using a w.d but the issue is that not all values are the same and all I want to do is add a zero for where they are applicable without messing the format of the other values.
format lborres lbornlo lbornhi z7.2.;
If your variable is numeric you can apply a standard format and that would add the leading zero.
ie
format yourVariable 8.1;
If your variable is character then you can test if the first character is a period and add a 0 or you can convert it to a number and store it that way instead. Option 2 is illustrated first as the first option overwrites the variable so to avoid any issues with that it's shown after.
data want;
set yourInputDataSet;
*Option 2;
new_numeric_variable = input(yourVariable, 8.);
format new_numeric_variable 8.1;
*Option 1;
if yourVariable =: '.' then yourVariable = catt('0', yourVariable);
run;
And as always, if your variable is incorrectly formatted this way, I would check my data import step and see if I can fix it there instead of after the fact. This is especially true if you used PROC IMPORT on a text file, where you can easily control the variable format and types as they're read in.
Are you value all less than 1? eg.
.3
.002
.752
.056
If not you would have to create picture format for each unit (thousands, hundreds, tens etc)
If they are all less than 1, z5.3 I believe will give you - 5 being the total length and 3 the number of decimal points.
data test;
format A z5.3;
a=.3; output; /* 0.300 */
a=.002; output; /* 0.002 */
a=.752; output; /* 0.752 */
a=.056; output; /* 0.056 */
run;
I have a Actuals column like so:
ID | Airport
------------
A | 98.4
B | 98.0
C | 95.3
I'm attempting to format the numbers above into percentages for a front-end report. I have this written in a switch statement - for ease I'll just write the logic as an IF boolean.
example_measure =
VAR Nums = SELECTEDVALUES(Table[Actuals])
VAR FormatNums = IF(DIVIDE(ROUND(nums,1), nums) = 1,
format(nums,"0%"),format(nums,"0.0%")
-
RETURN
FormatNums
no matter what I do this always returns a number with a floating point value of 1f
so in my raw data of 98.0 I'm expecting the format to return "98%" rather than "98.0%"
the measures are used on individual cards, and are filtered so they can only ever show one value or blank, meaning they will only ever display one value on their own.
I've toyed with using if(nums = int(nums) which should evaluate to true for 98.0 but it I always get false.
There is a simpler way - just use built-in formatting codes:
Formatted Actuals =
VAR x = SELECTEDVALUE(Data[Actuals])
RETURN
FORMAT(x, "General Number") & "%"
Result:
Built-in style "General Number" handles your situation correctly, you just need to add % symbol to the formatted string. No need to test for whole numbers.
To convert a column/measure into percentage, you can simply divide the column by 100 and then format it as a percentage. Use the following steps:
Create a new column/measure: Perc_value = Table[Actuals]/100
Then go into the modelling tab, select the created column/measure and format it as a % and limit the number of decimal places to 0
This should give the view you are looking for. Hope this helps.
Edit:
You can use the below formula to achieve the desired result:
Column4 = IF('Table'[Column2]-ROUND('Table'[Column2],0)=0,FORMAT('Table'[Column2]/100,"0%"),FORMAT('Table'[Column2]/100,"0.0%"))
Replace Column2 withyour field and you should be good to go.
SAS newbie - trying to complete a practice exercise. I'm probably going to face palm myself once someone points out what I'm doing wrong, but for now, I can't tell what the issue is.
I have a datset with 3 variables: ID $ avgNumDonations DonationAmt.
I'm asked to create a subset (i'm doing it in my proc print statement) that contains no records with avgDonation below 20 and DonationAmt under a million.(I believe this is a trick question as there are no cases in the original data set that meet both criteria)
I wrote my where clause as follows:
where DonationAmt >= 1000000 and avgNumDonations >= 20
However, it seems to be acting as an OR statement instead of a AND statement, because my subset is eliminating ID's 45 and 78.
Can someone tell me what I'm missing? As I mentioned, no cases meet the criteria so I expected to have the same cases in my "subset".
I think you may be misunderstanding either the WHERE or AND/OR logic.
WHERE is an inclusion criteria. Almost all of your records meet this criteria, but not all. Note that with AND it does have to meet both of your criteria, if either is false the it is excluded. It sounds like you want an OR instead of AND.
So to determine records that are excluded, either criteria would be false. So look for records where numDonations < 20 - (ID 45) and DonationAmount< 1000000 - ID 78. So those two records would be excluded. Which is what you're seeing.
If both criterias should meet the conditions you have to use OR instead of AND:
data a;
id=12;
avgdon = 58.3;
sumdon=4833722;
output;
id=45;
avgdon = 15.3;
sumdon=14833722;
output;
id=56;
avgdon = 50.3;
sumdon=9833722;
output;
id=78;
avgdon = 39.3;
sumdon=833722;
output;
id=910;
avgdon = 28.3;
sumdon=2833722;
output;
run;
proc print data=a(where=(sumdon>=1000000 OR avgdon>=20));
run;
Otherwise it is correct to use AND. Then 2 rows are eliminated.
The NEGPARENw.d reads the values -2000 as (20,00) based on the w.d
is there anyway to do the same in SAS 9.1?
I read a value 00005000- as character value and then converted to numeric value
-5000
TEMP=000005000-
Temp= COMPRESS(TEMP,'-')
TEMP=-(INPUT(TEMP,16.2)) format NEGPARENw.d its not working
PRoc report;
.....
define temp /display format = NEGPAREN16.2
Run;
Thanks
NEGPARENw.d format exists in 9.1.3, so there's no particular reason it wouldn't work the same in 9.1.3 as it would in later versions.
Is there an equivalent of the Oracle NVL function in SAS?
For example, if I have a where clause in SAS that says:
where myVar > -2;
it is not going to include any rows that have myVar = .
If I want to treat missing values as zero, i have to say:
where myVar > -2 or missing( myVar )
I'd like to be able to do something like:
where NVL( myVar, 0 ) > -2 // or some SAS equivalent
Is there something like this in SAS?
The coalesce function should do the job.
where coalesce(myVar,0) > -2
I'm not sure if the function became available in SAS 9, so if you have a really old SAS version this might not work.
Using the coalesce function is the right way to do this.
But if you have an old version of SAS where coalesce isn't implemented, you can use this trick:
where sum(myVar,0) > -2
If you use the sum function in SAS for adding, any non-missing number in the summation will force the result to be non-missing.
Thus adding 0 with the sum function will transform a missing value to 0, and non-missing values will remain unaltered.
One thing that y can do is
like
array varlistname var1 var2 var3 varn;
if array <>. then output;
It will ouput data sets having non missing values