- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hai PG,
Thanx.. But the test value T,B,C are repeats ,then how it possible to out the observation(first.Test)???
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Also not missing(test) has to change since they are character values. try if trim(left(compress(test))) ne ''
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The MISSING function works both for numeric and character arguments. The function returns true for empty strings. - PG
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
but first.test then output; The code doesn't work properly.The test value not shown .i meant(T B C)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanx Reeza ![]()
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanx PG ![]()