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

How do I use the lag function or any other function for that matter... I want to convert my data from column a to column b c d depending upon the data in  a :

abcd
1
2
3
411
5
6
721
81
9
1021
111
12
1331
141
151
16
17
1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

Something like this?

data have;

     infile cards dlm='09'x truncover;

     input ID a;

     cards;

1         

2         

3         

4    1   

5         

6         

7    2   

8         

9         

10   2   

11        

12        

13   3   

14        

15        

;

data want;

     set have;

     retain _c _d;

     if a=1 then

           b=1;

     if a=2 then

           _c=_n_;

     if _n_<=_c+1 then

           c=1;

     if a=3 then

           _d=_N_;

     if _n_<=_d+2 then

           d=1;

     drop _:;

run;

Haikuo

View solution in original post

2 REPLIES 2
Haikuo
Onyx | Level 15

Something like this?

data have;

     infile cards dlm='09'x truncover;

     input ID a;

     cards;

1         

2         

3         

4    1   

5         

6         

7    2   

8         

9         

10   2   

11        

12        

13   3   

14        

15        

;

data want;

     set have;

     retain _c _d;

     if a=1 then

           b=1;

     if a=2 then

           _c=_n_;

     if _n_<=_c+1 then

           c=1;

     if a=3 then

           _d=_N_;

     if _n_<=_d+2 then

           d=1;

     drop _:;

run;

Haikuo

Ksharp
Super User
data have;
     infile cards  truncover;
     input ID a ;
     cards;
1         
2         
3         
4    1   
5         
6         
7    2   
8         
9         
10   2   
11        
12        
13   3   
14        
15        
;
run;
proc sql noprint;
 select count(a) into : n from have;
quit;
data want(drop=i id n);
 set have;
 array x{*} n1-n%left(&n) ;
 if not missing(a) then do;
  n+1;
  do i=1 to a;
   x{n}=1;output; a=.;
  end; end;
else output;
run;


Xia Keshan

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 685 views
  • 0 likes
  • 3 in conversation