I'm a beginner using SAS and I was tasked with creating a table that looks like the following:
Group A Group B All
A B C D E F G H I J K L M
Age
n
mean(sd)
median
min-max
Gender
n
fl
ml
Race
n
white
asian
hispanic
black
The blanks in the table are just the calculations based on the criteria. What I've considered doing is the following but it doesn't look right and there are errors. Is there anyway to fix this or use proc freq/ proc tabulate if it's easier:
Libname test '/home/user/username';
DATA test;
SET test.test(keep = GROUP LETTER AGE GENDER RACE);
RUN;
PROC SORT DATA = test;
BY GROUP LETTER AGE GENDER RACE;
PROC MEANS DATA = test;
CLASS GROUP LETTER;
VAR AGE GENDER RACE;
RUN;
PROC PRINT DATA = test;
TITLE 'Demographics';
RUN;
PROC PRINT DATA=test;
RUN;
PROC TRANSPOSE DATA = test.test;
OUT = test.test;
BY GROUP LETTER;
VAR GROUP LETTER;
RUN;
PROC REPORT DATA = asl.asl;
COLUMN GROUP LETTER AGE GENDER RACE;
DEFINE GROUP /DISPLAY 'Group';
DEFINE LETTER /DISPLAY 'Letter';
DEFINE AGE /DISPLAY 'Age';
DEFINE GENDER /DISPLAY 'Gender';
DEFINE RACE /DISPLAY 'Race';
RUN;
Related
This is my code, please take a look
I want to get total number of student of fail, pass, merit and distinction in semester, so i need to input particular semesterid to get the result, but sqlplus always ignore and show the result no row selected.
column course_name format a50
compute sum label 'TOTAL_STUDENTS' of fail on report
compute sum label 'TOTAL_STUDENTS' of pass on report
compute sum label 'TOTAL_STUDENTS' of merit on report
compute sum label 'TOTAL_STUDENTS' of distinction on report
TTITLE CENTER "LEARNER_SEMESTER_RESULT_ANALYSIS"
break on report
create or replace view pass_result as
SELECT c.coursename as course_name, sc.courseID as courseID, count(r.learnerid) as num_of_students, sc.semesterid
FROM semestercourse sc, assessment a, result r, course c
WHERE sc.cs_id = a.cs_ID and
c.courseID = sc.courseID and
a.assesmentID = r.assesmentID and
r.status = 'Pass'
group by c.coursename, sc.courseID, sc.semesterid;
create or replace view fail_result as
SELECT sc.courseID as courseID, count(r.learnerid) as num_of_students, sc.semesterid
FROM semestercourse sc, assessment a, result r
WHERE sc.cs_id = a.cs_ID and
a.assesmentID = r.assesmentID and
r.status = 'Fail'
group by sc.courseID,sc.semesterid;
create or replace view merit_result as
SELECT sc.courseID as courseID, count(r.learnerid) as num_of_students,sc.semesterid
FROM semestercourse sc, assessment a, result r
WHERE sc.cs_id = a.cs_ID and
a.assesmentID = r.assesmentID and
r.status = 'Merit'
group by sc.courseID,sc.semesterid;
create or replace view distinction_result as
SELECT sc.courseID as courseID, count(r.learnerid) as num_of_students,sc.semesterid
FROM semestercourse sc, assessment a, result r
WHERE sc.cs_id = a.cs_ID and
a.assesmentID = r.assesmentID and
r.status = 'Distinction'
group by sc.courseID,sc.semesterid;
prompt Enter semester ID :
accept v_sid char
SELECT p.course_name, f.num_of_students as fail, p.num_of_students as pass,m.num_of_students as merit,d.num_of_students as distinction
FROM fail_result f
INNER JOIN pass_result p ON f.courseID = p.courseID and f.semesterID = p.semesterID
INNER JOIN merit_result m ON f.courseID = m.courseID and f.semesterID = m.semesterID
INNER JOIN distinction_result d ON f.courseID = d.courseID and f.semesterID = d.semesterID
where d.semesterID = '&v_sid';
To create a trigger before insert using Informix database.
When we try to insert a record into the table it should insert random alphanumeric string into one of the field. Are there any built in functions?
The table consists of the following fields:
empid serial NOT NULL
age int
empcode varchar(10)
and I am running
insert into employee(age) values(10);
The expected output should be something as below:
id age empcode
1, 10, asf123*
Any help is appreciated.
As already commented there is no existing function to create a random string however it is possible to generate random numbers and then convert these to characters. To create the random numbers you can either create a UDR wrapper to a C function such as random() or register the excompat datablade and use the dbms_random_random() function.
Here is an example of a user-defined function that uses the dbs_random_random() function to generate a string of ASCII alphanumeric characters:
create function random_string()
returning varchar(10)
define s varchar(10);
define i, n int;
let s = "";
for i = 1 to 10
let n = mod(abs(dbms_random_random()), 62);
if (n < 10)
then
let n = n + 48;
elif (n < 36)
then
let n = n + 55;
else
let n = n + 61;
end if
let s = s || chr(n);
end for
return s;
end function;
This function can then be called from an insert trigger to populate the empcode column of your table.
Am Geocoding hundreds of thousands of records, while this query is running if the address does not produce a Lat and Long value for a particular row it shows an error "invalid input syntax for integer: "J199" ". So if this line
(geocode_intersection(crashroad,crashreferenceroad,state,city,'',1)
Produces a value like "J199",it has to skip that row. So how to do this?
update nj.condition_3
set (rating,new_address,points) = ( COALESCE((g.geo).rating,-1),pprint_addy((g.geo).addy),st_astext(ST_SnapToGrid((g.geo).geomout, 0.000001)))
-- Replace in limit value if error occurs
FROM (SELECT addid FROM nj.condition_3 WHERE rating IS NULL ORDER BY addid LIMIT 3) As a
LEFT JOIN (SELECT addid, (geocode_intersection(crashroad,crashreferenceroad,state,city,'',1)) As geo
-- Replace in limit value if error occurs
FROM nj.condition_3 As ag WHERE ag.rating IS NULL ORDER BY addid LIMIT 3) As g ON a.addid = g.addid
WHERE a.addid = nj.condition_3.addid;
I have written a function to overcome this Error. So now it is working fine.
CREATE OR REPLACE FUNCTION geocode_all_values() RETURNS VOID AS
$$
DECLARE
r record;
g record;
BEGIN
FOR r IN select * from TableName where rating is null order by Sno
LOOP
BEGIN
FOR g IN select * from geocode_intersection(r.Street1,r.Street2,r.state,r.city,'',1)
LOOP
update TableName
set new_address = pprint_addy(g.addy),
rating = g.rating,
points = ST_AsTEXT(g.geomout)
where sno = r.sno;
END LOOP;
EXCEPTION WHEN OTHERS THEN
END;
END LOOP;
END;
$$
LANGUAGE plpgsql;
Input file with two column:
Visit ProductString
101 ;Cross Trainers;1;69.95,;Athletic Socks;10;29.99
102 ;Amplifier;1;120.90,;Headphone;2;59.99;leather wallet;1;99.99;
I am looking for Pig script that can parse "ProductString" value in each row and provide cumulative revenue.
ie.,Output:
69.95+29.99+120.90+59.99+99.99=380.82
I'll assume there should be a , after 59.99 and that there shouldn't be a ; after 99.99. If so, you need to tokenize and flatten on the , to extract products and then split on the ; to get item prices and qty.
Query:
data = LOAD 'db.table';
A = FOREACH data GENERATE visit, FLATTEN(TOKENIZE(product_string, ',')) AS tmp_col;
B = FOREACH A GENERATE visit, STRSPLIT(tmp_col, ';') AS prod;
C = FOREACH B GENERATE visit, prod.$1 AS item:chararray
, (int)prod.$2 AS qty:int, (double)prod.$3 AS revenue:double;
grpd = GROUP C all;
D = FOREACH grpd GENERATE SUM(C.revenue);
DUMP D;
Output:
(380.82)
Using Pig .12.
I want to use a FOREACH with a block statement; the result, via GENERATE depends on some value. I know that i can use Flatten with a statement in it. I.e.:
grunt> d = foreach j2 {
ord = order j1 by A::a1 desc;
l = limit ord 1;
generate flatten((IsEmpty(l)?'f':'g')); };
This works.
But what i want to do:
grunt> d = foreach j2 {
ord = order j1 by A::a1 desc;
l = limit ord 1;
generate flatten((l.$0=='2') ?'f':'g'); };
2014-05-07 22:28:08,750 [main] ERROR org.apache.pig.tools.grunt.Grunt
- ERROR 1200: mismatched input '?' expecting RIGHT_PAREN
Why does it say this?
I tried:
grunt> d = foreach j2 {
ord = order j1 by A::a1 desc;
l = limit ord 1;
generate flatten(((l.$0=='2') ?'f':'g')); };
2014-05-07 22:40:44,895 [main] ERROR
org.apache.pig.tools.grunt.Grunt - ERROR 1039:
(Name: Equal Type: null Uid: null)incompatible types in Equal Operator left hand side:bag :tuple(A::a1:chararray) right hand side:chararray
And i dont know how to resolve this.
I just want to generate my results in a test case, but first need to flatten the test condition.
Help ?
thanks,
Matt