Reputation: 45
I am a beginner in SAS and I have some datelines as
VIT_A 12 23 22 0 32 0 11
VIT_C 10 9 0 21 0 26 76
if I want to list only the datas where VIT_A ^=0
, VIT_C^=0
what will be the logic?
Results will be
VIT_A 12 23 22 32 11
VIT_C 10 9 21 26 76
Upvotes: 0
Views: 54
Reputation: 1804
If your data looks like this Table 1:
COL1 COL2
1 VIT_A VIT_C
2 12 10
3 23 9
4 22 0
You just need to used a where
clause
If your data looks like this Table [2]:
COL1 COL2
1 VIT_A VIT_C
2 12 23 22 0 32 0 11 10 9 0 21 0 26 76
Then you can use tranwrd
function to replace ' '
with ','
then replace '0'
with ''
Solution for table 1:
Data:
data have;
input VIT_A VIT_C;
datalines;
12 10
23 9
22 0
0 21
3 20
0 26
11 76
;
run;
Create two new tables without any 0
values and add row number in as obs
:
proc sql;
create table VIT_A as
select monotonic() as obs , VIT_A from have where VIT_A ne 0 ;
create table VIT_C as
select monotonic() as obs ,VIT_C from have where VIT_C ne 0;
quit;
Merge the two tables:
data want;
merge VIT_A VIT_C;
by obs;
run;
Output:
obs=1 VIT_A=12 VIT_C=10
obs=2 VIT_A=23 VIT_C=9
obs=3 VIT_A=22 VIT_C=21
obs=4 VIT_A=3 VIT_C=20
obs=5 VIT_A=11 VIT_C=26
obs=6 VIT_A=. VIT_C=76
Upvotes: 1
Reputation: 51611
Just use a WHERE statement?
proc print data=have ;
where vit_a ne 0 ;
run;
If you want to test multiple variables then make sure to combine them with the right logic.
where vit_a ne 0 AND vit_c ne 0;
Is different than
where vit_a ne 0 OR vit_c ne 0;
Upvotes: 0
Reputation: 1804
You can transpose your data, then select none zero values into a macro variable.
Assuming the values are in columns not in rows:
data have;
length string $100. ;
format string $100. ;
input string $ v1 v2 v3 v4 v5 v6 v7;
datalines;
VIT_A 12 23 22 0 32 0 11
VIT_C 10 9 0 21 0 26 76
;
run;
proc transpose data=have out=trans name=value;
by string;
run;
proc sql noprint ;
select COL1 into :VIT_A separated by ' ' from trans where string="VIT_A" and COL1 <> 0;
select COL1 into :VIT_C separated by ' ' from trans where string="VIT_C" and COL1 <> 0;
quit;
Data Want;
string='VIT_A';
Values="&VIT_A.";
output;
string='VIT_C';
Values="&VIT_C.";
output;
run;
Output:
string=VIT_A Values=12 23 22 32 11
string=VIT_C Values=10 9 21 26 76
Upvotes: 0