I have a question on format in SAS. Below is my code
PROC SQL;
CREATE TABLE OUT.FIN_POP_4a AS
SELECT sum(CORR_AM) as correction_am
FROM OUT.FIN_POP_4
;
QUIT;
correction_am is format numeric 8. How do I change this to Comma10.2 or Dollar10.2 in SAS in data step or proc sql code? I have tried various methods that did not work.
I have tried below code which worked but did not retain the two decimal places.
PROC SQL;
CREATE TABLE OUT.FIN_POP_4a AS
SELECT sum(CORR_AM) as correction_am format comma10.
FROM OUT.FIN_POP_4
;
QUIT;
you need formatw.d but you have only formatw. w = width d = decimal. So you do not have any decimal part that is why you do not see decimals. See below example and try both of them. first one gives no decimals and second gives you decimal values.
/* no decimal part i.e comma10.*/
PROC SQL;
CREATE TABLE FIN_POP_4a
AS SELECT sum(weight) as correction_am format= comma10.
FROM sashelp.class ; QUIT;
/* decimal part i.e comma10.2*/
PROC SQL;
CREATE TABLE FIN_POP_4a
AS SELECT sum(weight) as correction_am format= comma10.2
FROM sashelp.class ; QUIT;
use format comma10.2 instead of format comma10. ...
You may need to increase length as well since if you have millions, billions, or trillions or dollars then there is an increase in length ie comma15.2 or comma20.2. Below recommendation:
PROC SQL; CREATE TABLE FIN_POP_4a AS SELECT sum(weight) as correction_am format= comma15.2 FROM sashelp.class ; QUIT;
Related
I've created a user defined format by using proc format statements.Would like to create a macro over it in a way that if the input data changes, the code should able to do change accordingly.
Here is the code:
proc format ;
value $a 1='1-sepstrata'
0='0-Non-sepstrata'
A='A-sepstrata';
run;
In the dateset I've,a columns named stratum which has unique values such as 1,0,A.
Select the distinct values of STRATA and use it to generate the format definition in a file. Then use PROC FORMAT to create the format.
proc sql;
create table fmtdef as
select '$A' as fmtname
, strata as start
, catx('-',strata
,case when (strata='0') then 'Non-sepstrata' else 'sepstrata' end
) as label
from have
group by strata
order by fmtname,start
;
quit;
proc format lib=work.formats cntlin=fmtdef;
run;
So I have a rather interesting problem. I am trying to insert a current date in specific formats and styles, but for some reason it seems to fail. I know its not a formatting issue... But idk how to fix it. a data step solution is welcomed as well... Here's what works.
proc sql;
create table work.test
(test_Id char(50), test_Name char(50), cur_Mo char(1), cur_Qtr char(1), entered_Date char(8));
insert into work.test
values('201703','2017 Mar','0','0','24APR17')
values('201704','2017 Apr','0','0','24APR17')
values('201706','2017 Jun','1','0','23JUN17');
quit;
Here's what doesn't:
proc sql;
insert into work.test
values(catx('',put(year(today()),4.),case when month(today())< 10 then catx('','0',put(month(today()),2.)) else put(month(today()),2.)end) ,catx(' ',[put(year(today()),4.),put(today(),monname3.))],'1','0',put(today(),date7.));
quit;
You can use the %SYSFUNC() macro function to call most other SAS function in macro code. So to generate today's date in DATE7 format you could use:
insert into work.test (date)
values("%sysfunc(date(),date7)")
;
The way I'd probably do it is to use a data step to make a dataset that you would insert, and then insert that dataset.
You can use insert into (...) select (...) from (...) syntax in SAS, and the data step is much more flexible as to allowing you to define columns.
For example:
proc sql;
create table class like sashelp.class;
quit;
proc sql;
insert into class
select * from sashelp.class;
quit;
Or you can specify only certain variables:
proc sql;
insert into class (name, age)
select name, age from sashelp.class;
quit;
data to_insert;
name= 'Wilma';
sex = 'F';
age = 29;
height = 61.2;
weight = 95.3;
run;
proc sql;
insert into class
select * from to_insert;
quit;
Just make sure you either explicitly list the variables to insert/select, or you have the order exactly right (it matches up by position if you use * like I do above).
I am a newbie to SAS Base, and I am struggling to create a simple program that extracts data from a table on my database, runs e.g. PROC MEANS, and writes the data back to the table.
I know how to use PROC SQL (read and update tables) and PROC MEANS, but I can't figure out how to combine the steps.
PROC SQL;
SELECT make,model,type,invoice,horsepower
FROM
SASHELP.CARS
;
QUIT;
PROC Means;
RUN;
What I want to accomplish is create an additional column in the dataset with e.g. the mean of the horsepower.. and in the end I want to write that computed column to the table on the database.
Edit
What I was looking for is this:
PROC SQL;
create table want as
select make,model,type,invoice,horsepower
, mean(horsepower) as mean_horsepower
from sashelp.cars
;
QUIT;
PROC MEANS DATA=want;
RUN;
SAS makes this very easy to do with SQL since it will automatically remerge summary statistics back to detailed records.
create table want as
select make,model,type,invoice,horsepower
, mean(horsepower) as mean_horsepower
from sashelp.cars
;
Or using normal SAS code.
proc means data=sashelp.cars nway noprint ;
var horsepower ;
output out=mean_horsepower mean=mean_horsepower ;
run;
data want ;
set sashelp.cars ;
if _n_=1 then set mean_horsepower (keep=mean_horsepower);
run;
When doing a proc sql select into, I get the list of formatted values.
proc sql noprint;
select germ
into :oklist separated by '","'
from maxposition
where max <=10
;run;quit;
%put oklist=("&oklist");
oklist=("B. pertussis","Campylobacter","C. trachomatis","E. coli (VTEC)","Giardia","L.
pneumophila","Salmonella","Hepatitis A virus","Hepatitis B virus","Hepatitis C virus","Influenza
virus","Mumps virus")
How can you list the unformatted values instead?
(I mean without changing or removing the format)
Thanks!
You can use a format= statement in the proc sql to assign a generic format.
proc sql noprint;
select germ format=$20.
into :oklist separated by '","'
from maxposition
where max <=10
;run;quit;
%put oklist=("&oklist");
I know in teradata or other sql platforms you can find the count distinct of a combination of variables by doing:
select count(distinct x1||x2)
from db.table
And this will give all the unique combinations of x1,x2 pairs.
This syntax, however, does not work in proc sql.
Is there anyway to perform such a count in proc sql?
Thanks.
That syntax works perfectly fine in PROC SQL.
proc sql;
select count(distinct name||sex)
from sashelp.class;
quit;
If the fields are numeric, you must put them to character (using put) or use cat or one of its siblings, which happily take either numeric or character.
proc sql;
select count(distinct cats(age,sex))
from sashelp.class;
quit;
This maybe redundant, but when you mentioned "combination", it instantly triggered 'permutation' in my mind. So here is one solution to differentiate these two:
DATA TEST;
INPUT (X1 X2) (:$8.);
CARDS;
A B
B A
C D
C D
;
PROC SQL;
SELECT COUNT(*) AS TOTAL, COUNT(DISTINCT CATS(X1,X2)) AS PERMUTATION,
COUNT(DISTINCT CATS(IFC(X1<=X2,X1,X2),IFC(X1>X2,X1,X2))) AS COMBINATION
FROM TEST;
QUIT;