Hi I am having trouble with this question related to a school project. Converting values to a character missing value.
Run the following program to create the Dataset NOTAPPLY.
DATA NOTAPPLY;
LENGTH A B C D E $ 2;
INPUT ID A $ B $ C $ D $ E $ X Y Z;
DATALINES;
001 Y N N Y Y 1 2 3
002 na NA Y Y Y 3 4 5
003 NA NA NA na na 8 9 10
;
In the SAS data set NOTAPPLY, a value of either NA or na was used in place of a
missing value for all character variables. Create a new SAS data set NEW where
these values are converted to a character missing value.
There are many ways of converting values in SAS. One of them is using an INFORMAT by importing data. So you code might look like:
proc format;
invalue $MYMISS
'NA'=' '
'na'=' '
;
run;
DATA NEW;
LENGTH A B C D E $ 2;
INPUT ID A:$MYMISS2. B:$MYMISS2. C:$MYMISS2. D:$MYMISS2. E:$MYMISS2. X Y Z;
DATALINES;
001 Y N N Y Y 1 2 3
002 na NA Y Y Y 3 4 5
003 NA NA NA na na 8 9 10
;
run;
I have the following SAS dataset.
correlation
policynum
risknum
A
X
Y
A
X
Y
A
X
Y
B
X
Y
B
X
Y
B
X
Y
B
X
L
B
X
L
B
X
L
C
Z
M
C
Z
M
C
Z
M
D
Z
M
D
Z
M
D
Z
M
In SAS, I want to filter the above dataset so I get my final output as:
correlation
policynum
risknum
B
X
Y
B
X
Y
B
X
Y
B
X
L
B
X
L
B
X
L
D
Z
M
D
Z
M
D
Z
M
i.e. for each group of policynum and risknum, if multiple values exist for correlation, I want to keep the second value and get rid of the first value.
If only a single value of correlation exists for a group of policynum and risknum, I want to retain that group in my final output too.
What would be the best way to do this? It might be something simple as I am relatively new to SAS.
Thanks in advance!
If the order of the correlation values, in sort order, is the same ordering as they appear row-wise in the data set you can use SQL. Otherwise, SQL, being based on set theory, which does not have implicit row numbers, can not be used. A DATA step with DOW loop can be used.
Example:
FYI, one common situation in which SAS coders use the phrase 'DOW loop' is when SET & BY statements occur inside a DO loop.
data have;
input correlation $ policynum $ risknum $;
datalines;
A X Y
A X Y
A X Y
B X Y
B X Y
B X Y
B X L
B X L
B X L
C Z M
C Z M
C Z M
D Z M
D Z M
D Z M
;
/* keep last group of a nested group */
* SQL can be used only if correlation wanted is ALWAYS highest valued correlation;
proc sql;
create table want as
select * from have
group by policynum, risknum
having correlation = max(correlation)
;
* DATA Step DOW loops can be used when correlation wanted is last occurring correlation within by group;
data want;
do _n_ = 1 by 1 until (last.policynum);
set have;
by policynum risknum notsorted; /* presume at least contiguous */
end;
_want_correlation = correlation;
do _n_ = 1 to _n_;
set have;
if _want_correlation = correlation then OUTPUT;
end;
run;
I would like to add a new column to a dataset but I am not sure how to do so. My dataset has a variable called KEYVAR (character variable) with three different values. A participant can appear multiple times in my dataset, with each row containing a similar or different value for KEYVAR. What I want to do is create a new variable call NEWVAR that counts how many times a participant has a specific value for KEYVAR; when a participant does not have an observation for that specific value, I want NEWVAR to have a result of zero.
Here's an example of the dataset I would like (in this example, I want to count every instance of "Y" per participants as newvar):
have
PARTICIPANT KEYVAR
A Y
A N
B Y
B Y
B Y
C W
C N
C W
D Y
D N
D N
D Y
D W
want
PARTICIPANT KEYVAR NEWVAR
A Y 1
A N 1
B Y 3
B Y 3
B Y 3
C W 0
C N 0
C W 0
D Y 2
D N 2
D N 2
D Y 2
D W 2
You can use Proc SQL to compute an aggregate result over a group meeting a criteria, and have that aggregate value automatically merged into the result set.
-OR-
Use a MEANS, TRANSPOSE, MERGE approach
Sample Code (SQL)
data have;
input ID $ value $; datalines;
A Y
A N
B Y
B Y
B Y
C W
C N
C W
D Y
D N
D N
D Y
D W
E X
;
proc sql;
create table want as
select ID, value
, sum(value='Y') as Y_COUNT /* relies on logic eval 'math' 0 false, 1 true */
, sum(value='N') as N_COUNT
, sum(value='W') as W_COUNT
from have
group by ID
;
Sample Code (PROC and MERGE)
* format for PRELOADFMT and COMPLETETYPES;
proc format;
value $eachvalue
'Y' = 'Y'
'N' = 'N'
'W' = 'W'
other = '-';
;
run;
* Count how many per combination ID/VALUE;
proc means noprint data=have nway completetypes;
class ID ;
class value / preloadfmt;
format value $eachvalue.;
output out=freqs(keep=id value _freq_);
run;
* TRANSPOSE reshapes to wide (across) data layout, one row per ID;
proc transpose data=freqs suffix=_count out=counts_across(drop=_name_);
by id;
id value;
var _freq_;
where put(value,$eachvalue.) ne '-';
run;
* MERGE;
data want_way_2;
merge have counts_across;
by id;
run;
I have a series of string values with missing observations. I would like to use flat substitution. For instance variable x has 3 available values. There should be a 33.333% chance that a missing value will be assigned to the available values for x under this substitution method. How would I do this?
DATA have;
INPUT id a $ b $ c $ x;
CARDS;
1 Y Male . 5
2 Y Female . 4
3 . Female Tall 4
4 Y . Short 2
5 N Male Tall 1
;
Run;
You could use temporary arrays to store the possible values. Then generate a random index into the array.
DATA have;
INPUT id a $ b $ c $ x;
CARDS;
1 Y Male . 5
2 Y Female . 4
3 . Female Tall 4
4 Y . Short 2
5 N Male Tall 1
;
data want ;
set have ;
array possible_b (2) $8 ('Male','Female') ;
if missing(b) then b=possible_b(1+int(rand('uniform')*dim(possible_b)));
run;
I did this with generating random numbers and hard coding the limits. There should be an easier way to do this, but for the purposes of the question this should work.
option missing='';
data begin;
input a $;
cards;
a
.
b
c
.
e
.
f
g
h
.
.
j
.
;
run;
data intermediate;
set begin;
if a EQ '' then help= rand("uniform");
else help=.;
run;
data wanted;
set intermediate;
format help populated.;
if a EQ '' then do;
if 0<=help<0.33 then a='V1';
else if 0.33<=help<0.66 then a='V2';
else if 0.66<=help then a='V3';
end;
drop help;
run;
I'm here asking for help for a problem with proc transpose.
I have a dataset made this way (I'm going to show only 3 variables but I have lots of them)
PR ID VAR1a VAR1b VAR1c VAR2a VAR2b VAR2c VAR3a VAR3b VAR3c
1 1 x x x x x x x x x
1 2 x x x x x x x x x
1 3 x x x x x x x x x
2 1 x x x x x x x x x
2 2 x x x x x x x x x
2 3 x x x x x x x x x
I need an output dataset like this:
PREID ID VAR(name) A B C
1 1 VAR1(name) x x x
1 1 VAR2(name) x x x
1 1 VAR3(name) x x x
1 2 VAR1(name) x x x
1 2 VAR2(name) x x x
1 2 VAR3(name) x x x
1 3 VAR1(name) x x x
1 3 VAR2(name) x x x
1 3 VAR3(name) x x x
etc with preid 2 id 1 2 3, preid 3 id 1 2 3.
So I need to transpose but using the name (discriminating from a b c), I really have no idea from where I could start.
Can you help me please?
If i'm able to understand the output correctly. I think to achieve the result, first each observation of your input data would be broken into several different observation. So single observation would be converted into 9(var1a to var3c) observations( You can achive that using proc transpose by pr & id variable and transpose var1a to var3c variables). After this using a datastep, you would need to break _NAME__ variable into var1/2/3 and the a/b/c. After getting this done, you should be able to transpose the data to achieve your result.
I tried to write down the code based on your input data. Let me know if it helps.
data input;
infile datalines dsd dlm=',' missover;
input PR :$8.
ID :$8.
VAR1a :$8.
VAR1b :$8.
VAR1c :$8.
VAR2a :$8.
VAR2b :$8.
VAR2c :$8.
VAR3a :$8.
VAR3b :$8.
VAR3c :$8.;
datalines4;
1,1,x,x,x,x,x,x,x,x,x
1,2,x,x,x,x,x,x,x,x,x
1,3,x,x,x,x,x,x,x,x,x
2,1,x,x,x,x,x,x,x,x,x
2,2,x,x,x,x,x,x,x,x,x
2,3,x,x,x,x,x,x,x,x,x
;;;;
run;
proc transpose data=input out=staging ;
by pr id ;
var VAR1a--VAR3c;
run;
data staging;
set staging;
var=substrn(strip(_name_),1,length(strip(_name_))-1);
dummy=substrn(strip(_name_),length(strip(_name_)),1);
drop _name_;
run;
proc transpose data=staging out=final(drop=_name_);
by pr id var;
id dummy;
var col1;
run;
proc print data=final;run;
Similar to #sushil solution above, but one less step. Since you have to go into a data step anyways, you may as well transpose the data in that step as well. So in this solution the Proc Transpose/Data step are combined. If you had few enough variables I'd remove the last transpose as well, but this is more flexible if you have quite a few variables.
data input;
infile datalines dsd dlm=',' missover;
input PR :$8.
ID :$8.
VAR1a :$8.
VAR1b :$8.
VAR1c :$8.
VAR2a :$8.
VAR2b :$8.
VAR2c :$8.
VAR3a :$8.
VAR3b :$8.
VAR3c :$8.;
datalines4;
1,1,x,x,x,x,x,x,x,x,x
1,2,x,x,x,x,x,x,x,x,x
1,3,x,x,x,x,x,x,x,x,x
2,1,x,x,x,x,x,x,x,x,x
2,2,x,x,x,x,x,x,x,x,x
2,3,x,x,x,x,x,x,x,x,x
;;;;
run;
data out1;
set input;
array vars(*) var1a--var3c;
do i=1 to dim(vars);
name=vname(vars(i));
varname=substr(name,1,length(name)-1);
group=substr(name,length(name));
value=vars(i);
output;
end;
drop var1a--var3c;
run;
proc transpose data=out1 out=out2;
by pr id varname;
id group;
var value;
run;