BookmarkSubscribeRSS Feed
sahoositaram555
Pyrite | Level 9

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;

 

11 REPLIES 11
PaigeMiller
Diamond | Level 26

but it's throwing errors

 

Show us the log,  all parts, code, Notes, WARNINGS and ERRORS, with nothing chopped out.

 
--
Paige Miller
sahoositaram555
Pyrite | Level 9
25 GOPTIONS ACCESSIBLE;
26 data ph_visits1;
27 set t_ph_visits;
28 array visits{4} vis1-vis4;
ERROR: Alphabetic prefixes for enumerated variables (vis1-vis4) are different.
ERROR: Too few variables defined for the dimension(s) specified for the array visits.
29 array visitsc{4} $1 _6thvisit_F _18thvisit_F _30thvisit_F _42thweek_F;
30 do i=1 to 4;
31 if visits{i}=. then visitsc{i}="N";
ERROR: Undeclared array referenced: visitsc.
ERROR: Variable visitsc has not been declared as an array.
32 else if visits{i}=1 then visitsn{i}="Y";
ERROR: Undeclared array referenced: visitsc.
ERROR: Variable visitsc has not been declared as an array.
33 end;
34 run;
FreelanceReinh
Jade | Level 19

Hi @sahoositaram555,

 

Just correct the typo(s) visitc vs. visitsc.

sahoositaram555
Pyrite | Level 9
@FreelanceReinhard, Thanks for the pick. while few of the errors got resolved, below 2 errors are still existing.
ERROR: Alphabetic prefixes for enumerated variables (vis1-vis4) are different.
ERROR: Too few variables defined for the dimension(s) specified for the array visits.


FreelanceReinh
Jade | Level 19

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.

sahoositaram555
Pyrite | Level 9
I have found a way out, thank you very much. well initially there was this typo error then warnings helped me to resolve the issues.
The issue that remains is it is only working for vis1 perfectly, for other columns like vis2...vis4, the condition is not working, basically the newly formed columns are just blank, no warning, no errors. can you suggest what could be the issue? or if you could please tell how to make the loop run over columns then it would be helpful.
the modification that i have made to the code is below:
array old{*} vis: ;
array new{*} $ phvis1-phvis5;
do i=1 to dim(old);
if old[i]=. then new[i]="N";else new[i]="Y";
end;
PaigeMiller
Diamond | Level 26

Show us the log, all parts, code, Notes, WARNINGS and ERRORS, with nothing chopped out.

 
--
Paige Miller
Tom
Super User Tom
Super User

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.

HansGelders
Fluorite | Level 6

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
Pyrite | Level 9
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.
Tom
Super User Tom
Super User

@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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1037 views
  • 1 like
  • 5 in conversation