How to calculate this below
Used below formula in Microsoft excel
C = col1-col2- col3 + col4
I used below logic to convert above formula into SAS
C1 = col1-col2
C2 = sum(col3,col4)
C1= C1-C2
Still not able to match value with Microsoft excel output
You just need to wrap it in a datastep:
data new_data;
set old_data;
C = col1 - col2 - col3 + col4;
run;
Related
Col1 col2 Col3
1 Abc Shs
2 Cuz Dhsh
3 Uhhj Wer
. Xyz
. Pqr
4 Yui Pol
. Lkj
5 Haha Jaja
6 Euue Suus
7 Shus Yeye
I want to repeat col1 3rd record to be repeated in below two rows same for 4
Output I want
Col1 col2 Col3
1 Abc Shs
2 Cuz Dhsh
3 Uhhj Wer
3 uhhj Xyz
3 uhhj Pqr
4 Yui Pol
4 yui Lkj
5 Haha Jaja
6 Euue Suus
7 Shus Yeye
I tried using in Excel macros but not able achieve output in sas
Use retain to maintain and track the value of the variables retrieved or assigned in the prior row.
data want;
set have;
retain pcol1 pcol2 pcol3;
if missing(col1) then col1 = pcol1;
if missing(col2) then col2 = pcol2;
if missing(col3) then col3 = pcol3;
pcol1 = col1;
pcol2 = col2;
pcol3 = col3;
drop pcol:;
run;
Two arrays can be used for the case of many columns:
Variable based array for referencing values from data set
Temporary array for tracking prior row values. Temporary arrays are not affected by standard DATA Step behavior that 'reset PDV to missing'.
data want;
set have;
array priors(1000) _temporary_;
array values col1-col40;
do _n_ = 1 to dim(values); * repurpose _n_ for loop indexing;
if missing(values(_n_))
then values(_n_) = priors(_n_); * repeat prior value into missing value;
else priors(_n_) = values(_n_); * track most recent non-missing value;
end;
run;
I have a SAS dataset whose column layout is like this:
Col1 Col2 Col3
A_jan2018 A_feb2018 A_mar2018
B_jan2018 B_feb2018 B_mar2018
C_jan2018 C_feb2018 C_mar2018
I need to re-order the columns that start with A or B or C in such a format --
Col1 Col2 Col3
A_Jan2018 B_Jan2018 C_Jan2018
A_Feb2018 B_Feb2018 C_Feb2018
A_Mar2018 B_Mar2018 C_Mar2018
The A,B,C prefixes need not be in any sorting order (meaning they can start with anything), but my requirement is to re-order them based on the month-year (meaning B_Jan2018 A_Feb2018 C_2018 is okay).
Is there any way of achieving this in SAS?
Change the data structure so that you have a long dat set instead of
a short data set. (DATA STEP)
Separate out the prefix from date portion (DATA STEP)
Sort into your desired order (PROC SORT)
Transpose to desired format (PROC TRANSPOSE)
%*create sample data;
data have;
informat col1 col2 col3 $10.;
input Col1 $ Col2 $ Col3 $;
cards;
A_jan2018 A_feb2018 A_mar2018
B_jan2018 B_feb2018 B_mar2018
C_jan2018 C_feb2018 C_mar2018
;
run;
%*Make it wide table;
data _long;
set have;
array _col(3) col1-col3;
do i=1 to 3;
prefix=scan(_col(i), 1, "_");
date=input(scan(_col(i), 2, "_"), anydtdte.);
value=catx('_', prefix, put(date, monyy7.));
output;
end;
format date date9.;
run;
%*Sort by desired output;
proc sort data=_long;
by date prefix;
run;
%* transpose to the desired format;
proc transpose data=_long out=want1;
by i;
var value;
run;
If your data is exactly as posted and the output is transposed exactly then this also works but its entirely reliant on the source data being as specified and sorted correctly.
proc transpose data=have out=want2;
var col1-col3;
run;
I want to match the value in Column1 of one table with another and get the maximum value of column2.
For Example I have two tables,
Table 1
Col1 Col2
AA 17
AA 20
AB 10
AB 21
Table 2
Col1 Col2
AA ?
AB ?
I want my output to look like this,
Col1 Col2
AA 20
AB 21
I have tried,
Col2 = Max(Table1[col2])
but it didn't help. Thanks. Please share your thoughts.
You can use the following DAX:
Col2 =
CALCULATE(
MAX(Table1[Col2]),
FILTER(
Table1,
Table1[Col1] = Table2[Col1]
)
)
Result:
Data set in contains 4 columns col1-col4. I'm trying to create an output which separates 4 columns into two parts.
In the below code, by adding a fake variable blank, I can add one empty column between Part A and Part B.
options missing='';
proc report data=in missing
style(header)=[background=steelblue];
column ('Part A' col1 col2) blank ('Part B' col3 col4);
define blank/computed ' ' style=[background=white];
define col1 / display style[background=tan];
...
compute blank;
blank = .;
call define(_col_,'style','style={background=white borderbottomcolor=white}');
endcomp;
run;
The problem is I need
two different colors for spanning headers and the "original" headers.
the column between two spanning headers should be all white.
But the code is not able to the achieve 2nd purpose.
Current output looks like
1st row ------ Part A Part B (steelblue for entire row)
2nd row ------ col1 col2 col3 col4 (col1-col4 are tan, the column between col2 and col3 and white)
But the desired output is
1st row ------ Part A Part B (steelblue for Part A & B, but the column between them should be white)
2nd row ------ col1 col2 col3 col4 (col1-col4 are tan, the column between col2 and col3 and white)
I found this post but I can't even replicate Cynthia' output. The proc format seems doesn't work.
Proc Report - Coloring for Spanning Headers
This is fairly easy in excel - just insert a new empty column and no fill that column. How can I do this in SAS?
You don't mention ODS destination. This works for HTML and PDF(sort of).
I think the key assuming it actually does what you want is the use of 'a0'x the ascii non-breaking space. But this is not fully tested.
title;
options missing='';
proc format;
value $color
'a0'x = 'white'
other='steelblue'
;
proc report data=sashelp.class missing
style(header)=[background=$color. borderbottomcolor=$color.];
column ('Part A' name sex) ('a0'x blank) ('Part B' age weight height);
define _all_ / display style=[background=tan];
define blank / computed 'a0'x
style=[background=white borderbottomcolor=white]
style(header)=[background=white borderbottomcolor=white];
compute blank / char length=1;
blank = ' ';
call define(_col_,'style','style={background=white borderbottomcolor=white}');
endcomp;
run;
The code Cynthia published contains syntax errors (titles inside the proc report + missing ; on the style(header) lines).
With fixes, this works for me (SAS 9.3 AIX) :
proc format;
value $color
'REPORT' = '#9999FF'
'Australia' = '#FF6600'
'States' = 'pink'
'Wombat' = 'lightgreen'
other = 'lightblue';
value $altclr
'REPORT' = '#9999FF'
'Australia' = '#FF6600'
'States', 'Height', 'Weight' = 'pink'
'Wombat', 'Name', 'Age', 'Sex' = 'lightgreen'
other = 'lightblue';
run;
ods listing close;
ods tagsets.excelxp file = "%SYSFUNC(pathname(work))./Test.xml"
options ( embedded_titles='yes') style = sansprinter;
title 'All Headers Different Colors Based on Formats';
proc report data = sashelp.class(obs=3) nowd
style(header) = { background = $color. font_size= 10pt };
column ('REPORT'('Australia' ('Wombat' name age sex )('States' height weight)));
run;
title 'Some Headers Same Colors Based on Formats (one header diff)';
proc report data = sashelp.class(obs=3) nowd
style(header) = { background = $altclr. font_size= 10pt };
column ('REPORT'('Australia' ('Wombat' name age sex )('States' height weight)));
define name / 'Name';
define age / 'Age';
define sex / 'Sex';
define height / 'Height';
define weight / 'Weight' style(header)={background=lightyellow};
run;
ods _all_ close;
Suppose I have two files named "test" and "lookup".
The file "test" contains the following information:
COL1 COL2
az ab
fc ll
gc ms
cc ds
And the file "lookup" has:
VAR
ll
dd
cc
ab
ds
I want to find those observations, which are in "test" but not in "lookup" and to replace them with missing values. Here is my code:
data want; set test;
array COL[2] COL1 COL2;
do n=1 to 2;
if COL[n] in lookup.VAR then COL[n]=COL[n];
else COL[n]=.;
end;
run;
I tried the above code. But ERROR shows that "Expecting an relational or arithmetic operator".
My question is how to refer a variable from another file?
First, grab the %create_hash() macro from this post.
You need to use a hash object to achieve what you are looking for.
The return code from a hash lookup is zero when found and non-zero when not found.
Character missing values are not . but "".
data want;
set have;
if _n_ = 1 then do;
%create_hash(lu,var,var,"lookup");
end;
array COL[2] COL1 COL2;
do n=1 to 2;
var = col[n];
rc = lu.find();
if rc then
col[n] = "";
end;
drop rc var n;
run;
Here is an alternative approach using proc sql:
proc sql;
create table want as
select case when col1 in (select var from lookup) then '' else col1 end as col1,
case when col2 in (select var from lookup) then '' else col2 end as col2
from test;
quit;