Hi, I have a dataset. I'm supposed to use array to solve this issue.So i have to create 4 new variables based on these vis variables.ex: if vis1=1 then visit1="Y"......and so on. I have written a code below but it's throwing errors . i would be grateful if someone help me know which step i'm going wrong fundamentally . I'm ready to receive feedback on improvising my array using skills, as i'm a beginner level learner.
data a;
length subjid 5 vis1 3 vis2 3 vis3 3 vis4 3 ;
input subjid vis1 vis2 vis3 vis4;
cards;
10111 1 1 . 1
10112 1 1 1 1
10113 1 . . .
10114 . 1 . 1
10115 . . . 1
;run;
data want;
set a;
array visits{4} vis1-vis4;
/*below step i'm using to create the new variables in the same order as above /*
array visitc{4} $1 _6thvisit_F _18thvisit_F _30thvisit_F _42thweek_F;
do i=1 to 4;
if visits{i}=. then visitsc{i}="N";
else if visits{i}=1 then visitsc{i}="Y";
end;
run;
but it's throwing errors
Show us the log, all parts, code, Notes, WARNINGS and ERRORS, with nothing chopped out.
The code in your log does not exactly match the code in your initial post. I copied the code from your initial post, corrected the typo and got no error messages.
Show us the log, all parts, code, Notes, WARNINGS and ERRORS, with nothing chopped out.
I suspect that those two things are related. Your first error was saying you where trying to make a enumerated variable list using start and stop names that had different prefix characters. Your second error seems to be that the OLD array that you defined using the VIS: variable list only has one variable. Look carefully at the names of your variables. Perhaps re-type the code that you think is saying
VIS1 - VIS4
to make sure that there are not any strange hidden characters.
For example you might have used a lowercase L instead of the digit 1. My mother used to do that when I got her a computer. She was used to typewriters that did not have a 1 key as the lowercase L could be used impress the same symbol onto the page.
I can't see the problem. I just copied your code and it worked as you wished.
data a;
length subjid $5 vis1-vis4 3 ;
input subjid $ vis1-vis4;
cards;
10111 1 1 . 1
10112 1 1 1 1
10113 1 . . .
10114 . 1 . 1
10115 . . . 1
;run;
data want(drop =i);
set a;
array visits{4} vis1-vis4;
array visitsc{4} $ _6thvisit_F _18thvisit_F _30thvisit_F _42thweek_F;
do i=1 to 4;
if visits{i}=. then visitsc{i}="N";
else if visits{i}=1 then visitsc{i}="Y";
end;
run;
proc print data=want;run;
Obs | subjid | vis1 | vis2 | vis3 | vis4 | _6thvisit_F | _18thvisit_F | _30thvisit_F | _42thweek_F |
1 | 10111 | 1 | 1 | . | 1 | Y | Y | N | Y |
2 | 10112 | 1 | 1 | 1 | 1 | Y | Y | Y | Y |
3 | 10113 | 1 | . | . | . | Y | N | N | N |
4 | 10114 | . | 1 | . | 1 | N | Y | N | Y |
5 | 10115 | . | . | . | 1 | N | N | N | Y |
@sahoositaram555 wrote:
thanks, i have figured out a way though, Putting * inside {} solves the problem other wise array out of bound exception problem pops in. Don't know why.
You can either tell SAS how many variables should be in the array. Or let it count. If you are letting it count you don't need put in anything except the name and the variable list.
You can either give it a list of variables or give it a count and let if generate the variable names.
You can use curly braces {}, square brackets [] or parentheses ().
So all of these statements create the same array named VIS that references the variables VIS1,VIS2,VIS3 and VIS4.
array vis [4];
array vis vis1-vis4;
array vis [*] vis1-vis4;
array vis {*} vis1-vis4;
array vis (*) vis1-vis4;
array vis vis1-vis4;
array vis [4] vis1 vis2-vis4;
array vis vis1 vis2-vis4;
array vis vis1 vis2 vis3 vis4;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.