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

Hi,,

data test;

input zone $ state $ amount;

cards;

ZONE1 AP 121

ZONE1 KA 125

ZONE1 TM 129

ZONE1 UP 150

ZONE2 QW 130

ZONE2 VF 126

ZONE2 CV 129

ZONE2 LK 135

ZONE3 PL 123

ZONE3 NJ 165

ZONE3 OI 123

ZONE4 QA 176

ZONE4 UY 143

;

RUN;

PROC PRINT;

RUN;

From above data i want to print every second state in each zone by using first. and last . variable concept....

Thanks in Advance.....

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Same way, but use lag2.  e.g.:

data want (drop=x);

  set test;

  by zone;

  x=ifn(first.zone,0,lag2(first.zone));

  if x;

run;

View solution in original post

10 REPLIES 10
Linlin
Lapis Lazuli | Level 10

data test;

input zone $ state $ amount;

cards;

ZONE1 AP 121

ZONE1 KA 125

ZONE1 TM 129

ZONE1 UP 150

ZONE2 QW 130

ZONE2 VF 126

ZONE2 CV 129

ZONE2 LK 135

ZONE3 PL 123

ZONE3 NJ 165

ZONE3 OI 123

ZONE4 QA 176

ZONE4 UY 143

;

RUN;

data want;

  set test;

  by zone;

  if first.zone then delete;

data want;

  set want;

  by zone;

  if first.zone;

run;

PROC PRINT;

RUN;

                                 Obs    zone     state    amount

                                  1     ZONE1     KA        125

                                  2     ZONE2     VF        126

                                  3     ZONE3     NJ        165

                                  4     ZONE4     UY        143

Linlin

art297
Opal | Level 21

I'm not sure if this is what you are looking for, but it gets each 2nd state using first.variable:

data test;

  input zone $ state $ amount;

  cards;

ZONE1 AP 121

ZONE1 KA 125

ZONE1 TM 129

ZONE1 UP 150

ZONE2 QW 130

ZONE2 VF 126

ZONE2 CV 129

ZONE2 LK 135

ZONE3 PL 123

ZONE3 NJ 165

ZONE3 OI 123

ZONE4 QA 176

ZONE4 UY 143

;

RUN;

data want (drop=x);

  set test;

  by zone;

  x=ifn(first.zone,0,lag(first.zone));

  if x;

run;

PROC PRINT;

RUN;

kuridisanjeev
Quartz | Level 8

its working...

but if i want 3rd state in each zone????

what should i do????

art297
Opal | Level 21

Same way, but use lag2.  e.g.:

data want (drop=x);

  set test;

  by zone;

  x=ifn(first.zone,0,lag2(first.zone));

  if x;

run;

MikeZdeb
Rhodochrosite | Level 12

hi ... here's another idea

just specify the number you want in the IF statement (1, 2 , 3, whatever)

data third (drop=count);

set test;

by zone;

count + (-first.zone*count) + 1;

if count eq 3;

run;

Linlin
Lapis Lazuli | Level 10

data test;

input zone $ state $ amount;

cards;

ZONE1 AP 121

ZONE1 KA 125

ZONE1 TM 129

ZONE1 UP 150

ZONE2 QW 130

ZONE2 VF 126

ZONE2 CV 129

ZONE2 LK 135

ZONE3 PL 123

ZONE3 NJ 165

ZONE3 OI 123

ZONE4 QA 176

ZONE4 UY 143

;

RUN;

data want;

  set test;

  by zone;

   if first.zone then count=1;else

   count+1;

if count=3;

proc print;run;

                             Obs    zone     state    amount    count

                              1     ZONE1     TM        129       3

                              2     ZONE2     CV        129       3

                              3     ZONE3     OI        123       3

Tom
Super User Tom
Super User

You could increment a counter at the first record for a state. Reset the counter to 1 at the start of a zone.

This will make it easy to select the 2nd, 3rd, ... state.

data want ;

   set have;

   by zone state;

   if first.zone then order=1;

   else order+first.state;

   if order =2 ;

run;

kuridisanjeev
Quartz | Level 8

Thank you....

one more question...

This is the file i impored from excel sheet..

                                        east        100

                                                      150

                                                      165

                                        west        230

                                                      430

                                                      230

i want to make this data like this...

                                        east        100

                                        east        150

                                        east        165

                                        west        230

                                        west        430

                                       west         230

slove this....

Tom
Super User Tom
Super User

This is a common task.  Solution is to make a new variable that will retain the last non-missing value.

Lets assume your variable's name is REGION.

Something like:

data want ;

   set have;

   retain _region;

   drop _region;

   if region = ' ' then region=_region;

   else _region = region;

run;

Linlin
Lapis Lazuli | Level 10

data have;

input region $ 1-4 amount 5-7;

cards;

east 100

     150

     165

west 230

     430

     230

;

data want (drop=hold);

   retain hold ;

   set have;

   if region ne ' ' then hold=region;

   if region eq ' ' then region=hold;

run;

proc print;run;

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
  • 10 replies
  • 1553 views
  • 7 likes
  • 5 in conversation