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

Hi,

My datset is as follows

obs   f1  f2  f3  f4

1      1    2    3  4

2      3    4   5   6

3     7    6    6   8

Now,

with respect to condition like if obs =3 and f4=8 i need to fetch the value 8.

how can i achieve this?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data x;
 input obs   f1  f2  f3  f4 ;
cards;
1      1    2    3  4
2      3    4   5   6
3     7    6    6   8
;
run;
data want(keep=f4);
 i=3;
 set x point=i; 
 output;
 stop;
run;


Ksharp

View solution in original post

12 REPLIES 12
sambasiva_ravuri_tcs_com
Calcite | Level 5

Can you please elabrate the question.

Regards,

S Ravuri.

shru
Calcite | Level 5

I need to fetch one value from the dataset with respect to column name obs and f4.

So that when i run the code i only get the value 8 as i mentioned.

Thanks

Ksharp
Super User
data x;
 input obs   f1  f2  f3  f4 ;
cards;
1      1    2    3  4
2      3    4   5   6
3     7    6    6   8
;
run;
data want(keep=f4);
 i=3;
 set x point=i; 
 output;
 stop;
run;


Ksharp

Haikuo
Onyx | Level 15

Or:

data want;

set x(firstobs=3 obs=3 keep=f4);

run;

Haikuo

Ksharp
Super User

Your code is not as efficient as mine.

My code used Directly Access Method. However yours is Sequential Access Method.

If the dataset were very large. My code is better. Smiley Happy

Ksharp

Haikuo
Onyx | Level 15

Of course! :smileysilly:

Eleenrose
Calcite | Level 5

Hi,

I am totally new in SAS programming. I need to calculate last 6 months of current month. and it should be in yyyymm format. (eg: 201308) I was able to get the current date. but cannot find a solution to update the table with last 6 months.

this is how I appraoched. Please help.

Data Month;

Cur_month=date();

Format Cur_month YYMMN.

;

/*may be something like this is what I need*/

/*Mon1 = Cur_month-1;*/

/*Mon2 = Cur_month-2;*/

/*Mon3=Cur_month-3;*/

/*I did not get the right value in these fields. I got something like  19582, 19582, 19580*/

I tried to format it after calculating which again gave the value 201308. but it should be 201307, 201306, 201305

Please help..

ballardw
Super User

Eleenrose;

It would have been better to create a new thread as your question has no apparent bearing on the this thread.

The function you are looking for to create shifts of a data is INTNX.

I think you want Mon1 = INTNX("month",Cur_month, -1) ; The is an alignment parametere that can follow the -1 but you don't say whether the Mon1 should have a date represeting the begining, end, middle of the month or align with the same day of the month as Cur_month.

Your problem stems from subracting one day from the date value. If you used an MMDDYY format you would have seen your MonX variables decreasing by one day.

Eleenrose
Calcite | Level 5

Thanks for the help.

I got what I wanted to do. I used Intnx and got it working

data test;

mon1 = intnx('month', date(), -1);

mon2 = intnx('month', date(), -1);

.

.

.

.

format mon1, mon2,mon3.. yymmn6. ;

put (_all_)(=);

run;

Thanks everybody for the help. I am totally new to this field and trying to learn as far as I can..:)

Thanks again..

Eleen

Vince28_Statcan
Quartz | Level 8

Ksharp,

If we are into code optimization, then you may want to review the position of the keep statement so that you don't "waste" memory allocation for a data vector of 5 variables when you only need one Smiley Wink

Eleenrose, the issue is that all dates formats are held by sas as the number of days since 1960jan01. As such, when you substrat a day to your variable, even though its format is yymmn, it actually substracts 1 day from the date numeric value and then converts it back as a yymmn format so if a given date value was on the 16th of the month, you would need to substract 16 to eventually see the formatted substraction result change month after formating.

Intnx is the primary sas function to allow you to go around this issue.

mon1 = intnx('month', cur_month, -1);

Depending on your desired result, you may want to check the documentation for the 4rth parameter. By default from the above, the result will be the equivalent of the first day of the previous month and not the "same day" of the previous month.

Vincent

shru
Calcite | Level 5

Hi Hai.kuo and Ksharp

It worked fine

Thankss

sambasiva_ravuri_tcs_com
Calcite | Level 5

Hi Ksharp, Hai.Kuo

Both worked well.

Regards,

S Ravuri.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 2354 views
  • 2 likes
  • 7 in conversation