DATA Step, Macro, Functions and more

Need to fetch a single value from the existing dataset

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Need to fetch a single value from the existing dataset

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?


Accepted Solutions
Solution
‎03-21-2012 07:00 AM
Super User
Posts: 10,020

Need to fetch a single value from the existing dataset

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


All Replies
Contributor
Posts: 42

Need to fetch a single value from the existing dataset

Can you please elabrate the question.

Regards,

S Ravuri.

Contributor
Posts: 32

Need to fetch a single value from the existing dataset

Posted in reply to sambasiva_ravuri_tcs_com

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

Solution
‎03-21-2012 07:00 AM
Super User
Posts: 10,020

Need to fetch a single value from the existing dataset

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

Respected Advisor
Posts: 3,156

Need to fetch a single value from the existing dataset

Or:

data want;

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

run;

Haikuo

Super User
Posts: 10,020

Need to fetch a single value from the existing dataset

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

Respected Advisor
Posts: 3,156

Need to fetch a single value from the existing dataset

Of course! :smileysilly:

Occasional Contributor
Posts: 8

Re: Need to fetch a single value from the existing dataset

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..

Super User
Posts: 11,343

Re: Need to fetch a single value from the existing dataset

Posted in reply to Eleenrose

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.

Occasional Contributor
Posts: 8

Re: Need to fetch a single value from the existing dataset

Posted in reply to Eleenrose

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..Smiley Happy

Thanks again..

Eleen

Super Contributor
Posts: 339

Re: Need to fetch a single value from the existing dataset

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

Contributor
Posts: 32

Need to fetch a single value from the existing dataset

Hi Hai.kuo and Ksharp

It worked fine

Thankss

Contributor
Posts: 42

Need to fetch a single value from the existing dataset

Hi Ksharp, Hai.Kuo

Both worked well.

Regards,

S Ravuri.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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