Hai
I have a doubt in data set programming
Here i can show my dataset.
ID | Test | 1_Test | 2_Test | 3_test |
---|---|---|---|---|
U1 | T | a | ||
U1 | T | b | ||
U1 | T | c | ||
U2 | B | i | ||
U2 | B | j | ||
U3 | C | k | ||
U3 | C | l | ||
U3 | C | m | ||
U3 | C | n |
I would like to become the Varible 4_Test as shown in below
ID | 4_test |
---|---|
U1 | T |
U1 | a |
U1 | b |
U1 | c |
U2 | B |
U2 | i |
U3 | j |
U3 | C |
U3 | k |
U3 | l |
U3 | m |
U3 | n |
Please Help me :smileyplain:
Regards
Krishna
This is tested based on the data you provided. Mistakes in original code include a bug in array declaration (character and missing brace) and extra output statement.
data have;
infile datalines missover;
length ID Test Test1-Test3 $8;
input ID Test Test1-test3;
datalines;
U1 T a
U1 T b
U1 T c
U2 B . i
U2 B . j
U3 C . . k
U3 C . . l
U3 C . . m
U3 C . . n
;
data want;
set have;
by ID ;
array t_(3) $ test1-test3;
test4=test;
if first.id then output;
do i=1 to 3;
test4=t_(i);
if not missing(test4) then output;
end;
keep ID test4;
run;
Have you tried a transpose?
Otherwise a data step is a good way, using an output statement to control the output.
data want;
set have;
array t_3) test1-test3;
test4=test;
output;
do i=1 to 3;
test4=t_(i);
output;
end;
keep ID test4;
run;
You will have to use variable names starting with alphabetic characters (1_test is not a valid name, change it to test_1), then expand your dataset with:
data myDataset;
infile datalines missover;
length ID Test Test_1-Test_3 $8;
input ID Test Test_1 Test_2 Test_3;
datalines;
U1 T a
U1 T b
U1 T c
U2 B . i
U2 B . j
U3 C . . k
U3 C . . l
U3 C . . m
U3 C . . n
;
data want;
set myDataset; by test notsorted;
array t Test_:;
if first.test then output;
do i = 1 to dim(t);
test = t{i};
if not missing(test) then output;
end;
keep ID test;
run;
proc print data=want noobs; run;
PG
Hai PG,
Thanx.. But the test value T,B,C are repeats ,then how it possible to out the observation(first.Test)???
Try replacing
set myDataset; by test notsorted;
by
set myDataset; by ID test notsorted;
That will output one observation for each consecutive ID with the same test.
PG
Also not missing(test) has to change since they are character values. try if trim(left(compress(test))) ne ''
The MISSING function works both for numeric and character arguments. The function returns true for empty strings. - PG
but first.test then output; The code doesn't work properly.The test value not shown .i meant(T B C)
This is tested based on the data you provided. Mistakes in original code include a bug in array declaration (character and missing brace) and extra output statement.
data have;
infile datalines missover;
length ID Test Test1-Test3 $8;
input ID Test Test1-test3;
datalines;
U1 T a
U1 T b
U1 T c
U2 B . i
U2 B . j
U3 C . . k
U3 C . . l
U3 C . . m
U3 C . . n
;
data want;
set have;
by ID ;
array t_(3) $ test1-test3;
test4=test;
if first.id then output;
do i=1 to 3;
test4=t_(i);
if not missing(test4) then output;
end;
keep ID test4;
run;
Thanx Reeza
data want;
set have;
by ID ;
array t_3) test1-test3;
test4=test;
if first.id then output;
do i=1 to 3;
test4=t_(i);
if not missing(test4) then output;
output;
end;
keep ID test4;
run;
Thanx PG
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.