BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cruise
Ammonite | Level 13

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User

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;
ballardw
Super User

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.

Tom
Super User Tom
Super User

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;
Reeza
Super User

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;

 

Cruise
Ammonite | Level 13

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? 

Reeza
Super User

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

Cruise
Ammonite | Level 13
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.
Reeza
Super User

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

Cruise
Ammonite | Level 13
Ok. Thanks Reeza.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 9 replies
  • 3397 views
  • 5 likes
  • 4 in conversation