Percentile if and - sas

I want to do a percentile if and to be able to calculate the percentile based on 2 conditions in proc sql in SAS
Proc sql;
Create table test
As select *
Pctl(75,value1); quit;

Related

How can I show the date in the format of date9 in this SQL process?

After I run this SAS SQL process. The date variable "EXVISDAT" showed in the results in a numerical way. How can I change the code to let the date variable "EXVISDAT" show in format of "date9."?
proc sql;
create table test2 as
select VISIT, Targetdays, Targetdays + '01JUL2019'd - 1 as EXVISDAT
from data_in.chrono_visits_lts14424
order by Targetdays
;
quit;
You can read about it in documentation.
In yours case, query will look like:
proc sql;
create table test2 as
select VISIT, Targetdays, Targetdays + '01JUL2019'd - 1 as EXVISDAT format=date9.
from data_in.chrono_visits_lts14424
order by Targetdays
;
quit;

Put everything into sas sql

I have two codes one proc sql and another proc and datastep. Both are interlinked datasets.
Below is the proc sql lines.
create table new as select a.id,a.alid,b.pdate from tb a inner join
tb1 act on a.aid =act.aid left join tb2 as b on (r.alid=a.alid) where
a.did in (15,45); quit;
Below is the proc and datasteps created from above datatset new.
proc sort data = new uodupkey;
by alid;
data new1;
set new;
format ddate date9.
dat1=datepart(today);
datno=input(number,20.);
key=_n_;
rename alid blid;
run;
proc sort data=new1 nodupkey;
by datno dat1;
run;
I need to put everything into single proc sql step.
You mention two data steps but I only see one.
Anyway, your data step and proc sort can indeed be written in one sql query (which you can then insert in your proc sql):
proc sql;
create table new1 as
select id
,alid as blid
,pdate
,datepart(today) as dat1
,input(number,20.) as datno
,monotonic() as key
from new1
group by datno, dat1
having key=min(key)
;
quit;
One remark though. Your data step expects variables called ddate,today and number in your input dataset new. If that dataset is supposed to be the result of your first sql query, then those variables don't exist and their values along with those of dat1 and datno in new1 will always be missing.
Also I assume you misspelled nodupkey on your proc sort.
EDIT: or, to have it all in the same query (if that's what you meant with "the same proc sql"):
proc sql;
create table new1 as
select id
,alid as blid
,pdate
,datepart(today) as dat1
,input(number,20.) as datno
,monotonic() as key
from (
select a.id,a.alid,b.pdate
from tb a
inner join tb1 act
on a.aid =act.aid
left join tb2 as b
on (r.alid=a.alid)
where a.did in (15,45)
)
group by datno, dat1
having key=min(key)
;
quit;

Extract data using SQL, analyse and write back to SQL table

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;

SAS differences in outcome between sql and proc means

I want to have a weighted average of some variable in a macro variable. My var is zindi&aa and my weight is wprm&aa
I am trying to make sense of two ways of doing it :
one with a proc sql
proc sql noprint;
select mean(zindi&aa. * wprm&aa.)/sum(wprm&aa.) into :Mean_zindi_aa
from Panel(where=(annee&ap.<="&nais18" ));
quit;
it yields me an average of 0.77
one with proc means
proc means data=Panel(where=(annee&ap.<="&nais18" ));
var zindi&ap. ;
weight wprm&ap ;
output out=mean_zindi&ap mean=moy&ap;
run;
and then a data _null_ part
which yields an average of around 20200 that seems to be the correct one
so my question is
what I am missing with the proc sql so that it does give an absurd result ?
is there a better way to obtain my macro variable &mean_zindi_aa
proc sql noprint;
select sum(zindi&aa. * wprm&aa.)/sum(wprm&aa.) into :Mean_zindi_aa
from Panel(where=(annee&ap.<="&nais18" ));
quit;
Try this. Looks like you are trying to do a mean on (zindi&aa. * wprm&aa.). If you need the weighted average the above should work. because weighted average = sum(weight*variable)/sum(weights)
Change your PROC SQL to:
proc sql noprint;
select SUM(zindi&aa. * wprm&aa.)/sum(wprm&aa.) into :Mean_zindi_aa
from Panel(where=(annee&ap.<="&nais18" ));
quit;
You need to SUM the product, not take the MEAN.

SAS enterprise guide summing by personal ID

I have a dataset which has multiple obs per person. I want to have each single record showing the sum of a variable per person ID. However I do not want to group the data into single personal IDs. I hope the example below explains my question
I want to create the column in bold. How to do this? In SAS EG (or SAS if necessary)?
ID...Var1...SUM
X.....10.......30
X.....20.......30
Y.....20.......80
Y.....20.......80
Y.....40.......80
Z.....30.......30
You can do this using either proc sql or proc means
more info:proc means
proc sql
proc sql:
proc sql noprint;
create table new_table as
select distinct id, var1, sum(var_to_sum) as summed_var_name
from old_table
group by id
;
quit;
after rereading your question, using proc means you will need to merge var1 back in, better off using proc sql above.
proc means:
proc means data = old_table sum;
by id var1;
var var_to_sum;
output out = new_table sum;
run;