Help using Base SAS procedures

A value combining/Concatenating question between records -Thanks

Reply
Contributor
Posts: 44

A value combining/Concatenating question between records -Thanks

Thanks for helping me with the one issue I had. This is one big complex problem so I am trying to solve by myself some parts but for
others I don't have any choice other than asking for your kind help. My input is very restricted can't anything there.

==================

Here is the issue:

==================

I have the following datasets with all character variables (F1, F2, F3, F4, F5, F6,F7):


   F1        F2         F3            F4          F5        F6        F7
-------    -------    -------      -------     -------   -------   -------
        
01.2012    01.2012    01.2013      01.2013     05.2014   05.2015   05.2017
sopyu      kopyu     sopyu         kopyu       sopyu     kopyu     sopyu
   0         1         5             7            8        0          1
   6         24        76            8            0        0          0


I need to combine (concatinate) first two observations value in such a way that for the variable F1:
'01.2012'  from first record and  'sopyu' from second record combines and makes 'sopyu_201201'.
The result dataset will look as below:

  
   F1               F2              F3                F4              F5           F6            F7
------------    -------------   ------------     ------------    ------------  ------------  ------------

sopyu_201201    kopyu_201201    sopyu_201301     kopyu_201301    sopyu_201405  kopyu_201505  sopyu_201705                                                
  0                 1              5                  7               8             0             1
  6                24             76                  8               0             0             0

Thnk you again for your kind help

Super User
Posts: 11,343

Re: A value combining/Concatenating question between records -Thanks

Posted in reply to need_sas_help

Is the current length associated with the variables F1 through F7 large enough to accept sopyu_201201? If the length is not at least 12 that's going to be the first step.

data want;

     length F1-F7 $ 12;

     set have;

     L1 = Lag(f1);

     L2 = Lag(f2);

     L3 = Lag(f3);

     L4 = Lag(f4);

     L5 = Lag(f5);

     L6 = Lag(f6);

     L7 = Lag(f7);

     array f f1-f7;

     array L L1-L7;

     if _n_ = 2 then do _i_=1 to 7;

          F[_i_] = cats(F[_i_],'_',substr(L[_i_],3),substr(L[_i_],1,2));

     end;

     if n ge 2 then output;

     drop L1-L7;

run;

If the data set is large enough to have significant performance issues as we are only manipulating two rows of data you could:

1) use an obs limit to make WANT with just the first two records

2) then Proc Append Base=want data=Have (firstobs=3) force; run; /* force may be needed if the lengths of the F variables are now different between the two sets.

Super User
Posts: 10,023

Re: A value combining/Concatenating question between records -Thanks

Posted in reply to need_sas_help
data have;
input (F1        F2         F3            F4          F5        F6        F7 ) (:$20.);
cards;
01.2012    01.2012    01.2013      01.2013     05.2014   05.2015   05.2017
sopyu      kopyu     sopyu         kopyu       sopyu     kopyu     sopyu
   0         1         5             7            8        0          1
   6         24        76            8            0        0          0
;
run;
data want(drop=i); 
 set have;
 array x{*} $ _character_;
 if _n_ le 2 then do;
  do i=1 to dim(x);
   x{i}=catx('_',x{i},cats(scan(lag(x{i}),2,'.'),scan(lag(x{i}),1,'.')));
  end;
 end;
 if _n_ ge 2;
run;

Xia Keshan

Ask a Question
Discussion stats
  • 2 replies
  • 159 views
  • 0 likes
  • 3 in conversation