BookmarkSubscribeRSS Feed
srikanthyadav44
Quartz | Level 8

Hi all,

my data set has N variables with are repeated for K number of times. 

Out of them, i have to select only one type of variable which is repeated for K number of times. and also the company name.( please note that first array of variables are not suffixed with serial number, only from second array, the serial number is suffixed like varA1, varB1 etc., )

the format of my data file is as follows

companyvarAvarB………..varNvarA1varB1………..varN1varAKvarBK………..varNK
1            
2            
3            
4            
.            
.            
.            
M            

 

thanks in advance 

8 REPLIES 8
KachiM
Rhodochrosite | Level 12
company	varA	varB	………..	varN	varA1	varB1	………..	varN1	varAK	varBK	………..	varNK

Do you want this?

company	varA1	varB1	………..	varN1	varAK	varBK	………..	varNK

 

srikanthyadav44
Quartz | Level 8

No. i want the like as follows

company varA varA1 varA2  varA3.................................. varAk  var

 

I want only variable array of variable A from A to AK

 

thanks in advance

srikanthyadav44
Quartz | Level 8

and company name also 

andreas_lds
Jade | Level 19

Please describe the problem you have and what you want to achieve.

The array statement is simple:

array arrayName List_The_Variables_Separated_By_Blank;

Note, that all variables in an array need to be of same type.

 

KachiM
Rhodochrosite | Level 12

Here is an illustration. Extend it to your needs.

data _null_;
   set have;
   array v varA -- varB3;
   do i = 1 to dim(v);
      if substr(vname(v[i]),4,1) = 'A' then put v[i]=;
   end;
run;

This writes to the log.

 

Another way is to catch the values of the selected values of variables into a space separated List as: 

data want;
   set have;
   array v varA -- varB3;
   length str $32767;
   call missing(str);
   do i = 1 to dim(v);
      if substr(vname(v[i]),4,1) = 'A' then str = catx(' ',str, v[i]);
   end;
keep company str;  
run;
Astounding
PROC Star

How about an easy way?

 

data want;
   set have;
   keep company varA: ;
run;
KachiM
Rhodochrosite | Level 12

I missed to show the 'HAVE' data set that was used in my previous posting. It is used again here.

 

An improved solution can be obtained by following the steps:

[1] Collect the variable names that match the pattern (varA) into a List.

[2] The List be sent as a macro variable in [1] itself.

[3] Declare an array with elements of &List.

[4] Automatically the selected variables are filled with their corresponding values.

 

Here is the program with input data set and the Output:

 

data have;
input company  varA varB varA1 varB1 varA2 varB2 varA3 varB3;
datalines;
10 1 2 3 11 21 12 22 31 32
20 2 3 4 22 23 24 25 26 27
;
run;

data _null_;
   length List $ 32767;
   set have;
   array v varA -- varB3;
   if _N_ = 1 then do;
      do i = 1 to dim(v);
         if substr(vname(v[i]),4,1) = 'A' then List = catx(' ',List, vname(v[i]));
      end;
      call symputx('List', List);
   end;
stop;   
run;

%put &List;



data want;
   set have;
   array vn &List;
keep company &List;   
run;


Obs company  varA varA1 varA2 varA3
1      10      1     3    21    22
2      20      2     4    23    25
Reeza
Super User

If the variables are in order, list the CompanyID, the first variable in the series and last variable in the series.

 

data want;
set have;

keep companyID varAK -- VarNK;

run;

https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

See this link for more options on how to list variables in an easier way.


@srikanthyadav44 wrote:

Hi all,

my data set has N variables with are repeated for K number of times. 

Out of them, i have to select only one type of variable which is repeated for K number of times. and also the company name.( please note that first array of variables are not suffixed with serial number, only from second array, the serial number is suffixed like varA1, varB1 etc., )

the format of my data file is as follows

company varA varB ……….. varN varA1 varB1 ……….. varN1 varAK varBK ……….. varNK
1                        
2                        
3                        
4                        
.                        
.                        
.                        
M                        

 

thanks in advance 


 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 2018 views
  • 3 likes
  • 5 in conversation