BookmarkSubscribeRSS Feed
kajal_30
Quartz | Level 8

Hi team, 

I am creating a new variable during reading the csv file but somehow not getting any idea to do so .so I need to create 2 variables one is the date_Curr which will get the current datetime() second column is yrmnt value will be &ymt.

can you please help.

Kajal

libname work2 '/work/sas/data';

data work2.sample;
	infile "/work/sas/sample.csv"
		delimiter = ","  
		dsd
		truncover
		missover
		firstobs=2;
	Length
		cust_id
		cust_name
	;
	informat  cust_id 8.;
	informat  cust_name $5.;
	format  cust_id 8.;
	format  cust_name $5.;
	input 
		cust_id
		cust_name
	;
run;

 

4 REPLIES 4
Reeza
Super User
libname work2 '/work/sas/data';

data work2.sample;
	infile "/work/sas/sample.csv"
		delimiter = ","  
		dsd
		truncover
		missover
		firstobs=2;
	Length
		cust_id
		cust_name
	;
	informat  cust_id 8.;
	informat  cust_name $5.;
	format  cust_id 8.;
	format  cust_name $5.;

       date_curr = datetime();
       format date_curr datetime.;
       yrmnt="&ymt.";

	input 
		cust_id
		cust_name
	;
run;

EDIT: added format for datetime. 

kajal_30
Quartz | Level 8

Thanks @Reeza  also is there a way if we can change the type of column cust_id in the same program ? or I need to write another step to modify the dataset? 

Reeza
Super User

You can change the type/format/length as you read it in and save it. 

 

I would highly suggest you comment your code so you understand what each section is doing, that makes it a bit easier to see where you need to modify it. 

 

Tom
Super User Tom
Super User

Just add any assignment statements you need to the DATA step.

 

Why are you naming variable DATE_CURR if you want it to contain DATETIME values instead of DATE values?  Either place the DATE into the variable or rename the variable.

Is the variable YMNT supposed to be numeric or character? What types of strings is the macro variable YMT going to contain?

 

  • You forgot to include the actual LENGTH values you wanted for the variables in the LENGTH statement.  Use a length of 8 for numeric variables and use $ followed by the maximum number of bytes to reserve for the character variables.
  • You do not need specify both the TRUNCOVER and MISSOVER option.  Do not use the ancient MISSOVER option as it could throw away short values at the end of the line that are too short for the informat used (if you modified the INPUT statement to use formatted input).
  • You don't need to specify comma as the delimiter when using the DSD option as that is the default delimiter when DSD is used.
  • There is no need to attach either INFORMAT or FORMAT specifications for either of those two variables.  SAS already knows how to read and write normal numbers and strings.
  • Make sure to attach appropriate formats to the date or datetime variables so the values (days or seconds) is displayed in a way that humans will understand.
  • If the macro variable YMT has values like 1234 then you can use it to make a numeric variable.  If you want to use it to create a character variable then define the variable as character in the LENGTH statement add quotes around the macro variable reference in the assignment statement.
libname work2 '/work/sas/data';

data work2.sample;
  infile "/work/sas/sample.csv"
    delimiter = ","  
    dsd
    truncover
    firstobs=2
  ;
  length
    cust_id 8
    cust_name $5
    curr_date 8
    curr_datetime 8
    ymnt 8
  ;
  input 
    cust_id
    cust_name
  ;
  curr_date = date();
  curr_datetime = datetime();
  ymnt = &ymt;
  format curr_date date9. curr_datetime datetime19.;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 4 replies
  • 1117 views
  • 1 like
  • 3 in conversation