My survey data has group variables such as A1-A10, B1-B2, C1-C5, D1-D15...so forth so on. And I'd like to exclude 😧 in following data step. 😧 has missing (.) . I don't need to shift 😧 by +1 to the right.
I'm using SAS 9.4.
data have1(drop=i); set have;
array shift[*] _numeric_;
do i=1 to dim(shift);
if shift(i) not in ('D:') then shift(i)=shift(i)+1;
end;
run;
data have1(drop=i); set have;
array shift[*] _numeric_;
do i=1 to dim(shift);
shift(i)=shift(i)+1;
end;
where _numeric_ not in ('D:');
run;
The codes above didn't work. Any suggestions please?
Thanks for your time.
Following up on @ballardw post here is a method to create the array without the "D" variables, but keep then in the dataset.
You can take advantage of the fact that variable lists values depend on where they appear in the data step. The _NUMERIC_ variable list will only find the vairables that have already beed defined. You can then SET the dataset again and read in all of the variables.
data want ;
set have (drop=D:);
array shift _numeric_;
set have ;
do over shift;
shift=shift+1;
end;
run;
I think you are asking how to conditionally execute based on the NAME of particular variable in a an ARRAY?
data want;
set have;
array shift _numeric_;
do i=1 to dim(shift);
if upcase(vname(shift(i))) ^=: 'D' then shift(i)=shift(i)+1;
end;
drop i;
run;
You would have to test the name of the variable.
Something like this is one way:
if upcase(substr( vname(shift[i]), 1)) ne 'D' then <whatever>;
In the following data step use drop on the SET statement if you don't want it:
data want ;
set have (drop= 😧 );
run;
The : says to consider all variables that start with D as a group and drop them. If you have other variables such as DATE then another
set have (drop= D1-D15) will use the listed variables and remove D1 through D15.
The concept here is variable list. There are several forms available in SAS.
Following up on @ballardw post here is a method to create the array without the "D" variables, but keep then in the dataset.
You can take advantage of the fact that variable lists values depend on where they appear in the data step. The _NUMERIC_ variable list will only find the vairables that have already beed defined. You can then SET the dataset again and read in all of the variables.
data want ;
set have (drop=D:);
array shift _numeric_;
set have ;
do over shift;
shift=shift+1;
end;
run;
In this case I would suggest also consider not using the list _numeric_.
You could either list them out separately or somehow.
array myvars (*) A: B: C: E: ... etc;
All worked out. What in general happens when you find more than one solutions worked out from multiple advisors? I probably have to post this in the community matter?
@SUNY_Maggie wrote:
All worked out. What in general happens when you find more than one solutions worked out from multiple advisors? I probably have to post this in the community matter?
I don't think it really matters. Pick the solution that's the closest to what you need. If you end up compiling things from multiple people it's also ok to post the full solution in one place and mark that as the answer. The 'goal' of answering should be to inform someone searching as to which answer is correct for this problem, and yes there may be more than one.
@SUNY_Maggie wrote:
I thought of compiling once and then wondered if i then select my own compiling as for solution would look like taking the credit of others' work.
There are situations where that is appropriate and others where it's not. If you do that for all your questions, it's likely you're not making the correct decision but I think you're worrying about this too much.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.